<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board
|   =============================================
|   by Matthew Mecham
|   (c) 2001 - 2006 Invision Power Services, Inc.
|   
|   =============================================
|   
|   
+---------------------------------------------------------------------------
|   > $Date: 2008-01-03 11:01:06 -0500 (Thu, 03 Jan 2008) $
|   > $Revision: 1155 $
|   > $Author: bfarber $
+---------------------------------------------------------------------------
|
|   > User Profile functions
|   > Module written by Matt Mecham
|   > Date started: 28th February 2002
|
|	> Module Version Number: 1.0.0
|   > DBA Checked: Thu 20 May 2004
|   > Quality Checked: Wed 15 Sept. 2004
+--------------------------------------------------------------------------
*/

/**
* Public Action Class: Profile
*
* @package		InvisionPowerBoard
* @subpackage	Public-Action
* @author		Matt Mecham
* @copyright	Invision Power Services, Inc.
* @version		2.1
*/

if ( ! defined( 'IN_IPB' ) )
{
	/**
	* Error checking
	*/
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
	exit();
}

/**
* Public Action Class: Profile
*
* @package	InvisionPowerBoard
* @subpackage	Public-Action
* @author   Matt Mecham
* @version	2.1
*/

class profile
{
    var $output     = "";
    var $page_title = "";
    var $nav        = array();
    var $html       = "";

    var $member     = array();
    var $m_group    = array();
    
    var $jump_html  = "";
    var $parser     = "";
    
    var $links      = array();
    
    var $bio        = "";
    var $notes      = "";
    var $size       = "m";
    
    var $show_photo = "";
    var $show_width = "";
    var $show_height = "";
    var $show_name  = "";
    var $common;
    var $photo_member = "";
    
    var $has_photo   = FALSE;
    
    var $lib;
    
    function auto_run()
    {
        
        
    	$this->ipsclass->load_template('skin_profile');
        $this->ipsclass->load_language('lang_profile');
        //-----------------------------------------
        // Load and config the post parser
        //-----------------------------------------
        require_once( ROOT_PATH . 'sources/action_public/Common.php' );
        $this->common = new Common();
        $this->common->ipsclass = & $this->ipsclass;
        $this->common->parseLastVisit();
        require_once( ROOT_PATH."sources/handlers/han_parse_bbcode.php" );
        $this->parser                      = new parse_bbcode();
        $this->parser->ipsclass            = $this->ipsclass;
        $this->parser->allow_update_caches = 1;
        
        $this->parser->bypass_badwords = intval($this->ipsclass->member['g_bypass_badwords']);
    	
        //-----------------------------------------
        // Load Text Code by BabyWolf
        //-----------------------------------------
        require_once( KERNEL_PATH . 'class_textcode.php');
        $class_textcode = new class_textcode();
        $this->ipsclass->textcode = $class_textcode;
		
    	//-----------------------------------------
    	// Require the HTML and language modules
    	//-----------------------------------------
    	
    	$this->ipsclass->load_language('lang_profile');
    	$this->ipsclass->load_template('skin_profile');
    	//$this->ipsclass->pageTitle = $this->ipsclass->lang['header_personal_info'];
        //echo $this->ipsclass->lang['header_personal_info'];exit();
    	$this->ipsclass->base_url_nosess = "{$this->ipsclass->vars['board_url']}/index.{$this->ipsclass->vars['php_ext']}";
    	//var_dump($this->ipsclass->input['CODE']);exit();
        
    	// Make safe input by BabyWolf
    	$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
    	//echo "code: ". $this->ipsclass->input['CODE'];
    	//-----------------------------------------
    	// What to do?
    	//-----------------------------------------
        
        if( ($this->ipsclass->member['id'] == "" || intval($this->ipsclass->member['id']) <= 0) && $this->ipsclass->input['CODE'] == "") {
            $this->ipsclass->input['CODE'] = "guest-view-profile";
        }
        //echo $this->ipsclass->input['CODE'];exit();
    	switch($this->ipsclass->input['CODE'])
    	{
            case "diaries":
                if(intval($this->ipsclass->input['f']) > 0 ){
                    $this->ipsclass->boink_it($this->ipsclass->vars['profile_url'] . '/' . $this->ipsclass->member['name'] . "/?CODE=diaries" );
                    exit();
                }
                $this->load_diaries();
                break;
            case 'guest-view-profile':
                $this->guest_view_profile();
                break;
            case '03':
                //echo "dfvdfdfd";exit();
//                if ( $this->ipsclass->vars['pp_show_classic'] )
//                {
//                $this->view_profile();
//                }
//                else
//                {
                //$this->page_title = $this->ipsclass->lang['page_title_pp'];
                $this->personal_portal_view();
//                }
                break;

            case 'showphoto':
                $this->show_photo();
                break;

            case 'showcard':
                $this->show_card();
                break;
            case 'own-comment':                             
                $this->owned_comment();
                break;
            case 'comment-paging':
                //echo "dfdfdf";//exit();
                $this->load_comments();
                break;
            case "load-all-friends":
                ///echo 'chug';exit();
                $this->load_all_friends();
                $this->nav = array($this->ipsclass->lang['page_title_all_friend']);
                $this->page_title = $this->ipsclass->lang['page_title_all_friend'];
                
                //$this->page_title = "Danh sach ban be";
                break;
            case "load-friend-profile":
                $this->friend_profile();
                break;
            case "delete-own-comment":
                $this->delete_owned_comment();
                break;
            case 'show-display-names':
                $this->show_display_names();
                break;

            // Added by BabyWolf
            case 'personal_ajax_reply_comment':
                $this->personal_ajax_reply_comment();
                break;

            case 'personal_ajax_load_dialog':
                $this->personal_ajax_load_dialog();
                break;

                case 'personal_ajax_load_tab':
                        $this->personal_ajax_load_tab();
                        break;

                case 'personal_ajax_add_comment':
                        $this->personal_ajax_add_comment();
                        break;

                case 'personal_ajax_delete_comment':
                        $this->personal_ajax_delete_comment();
                        break;

                case 'personal_ajax_reload_comments':
                        $this->personal_ajax_reload_comments();
                        break;

                case 'personal_portal_view':
//                        if ( $this->ipsclass->vars['pp_show_classic'] )
//                        {
//                        $this->view_profile();
//                        }
//                        else
//                        {
                    $this->ipsclass->pageTitle = $this->ipsclass->lang['page_personal_info'];
                    //echo $this->ipsclass->lang['header_personal_info'];exit();
                    $this->personal_portal_view();
                    $this->nav = array($this->ipsclass->lang['page_title_pp']);
                    $this->page_title = $this->ipsclass->lang['page_title_pp'];
//                        }
                        break;

                case 'personal_iframe_friends':
                        $this->personal_iframe_friends();
                        break;

                case 'personal_iframe_settings':
                        $this->personal_iframe_settings();
                        break;

                case 'personal_iframe_settings_save':
                        $this->personal_iframe_settings_save();
                        break;

                case 'personal_iframe_comments':
                        $this->personal_iframe_comments();
                        break;
                case 'personal_iframe_comments_save':
                        $this->personal_iframe_comments_save();
                        break;

                case 'friends_list_popup':
                    switch( $this->ipsclass->input['do'] )
                    {
                        default:
                        case 'list':
                                $this->friends_list_list();
                        break;
                        case 'add':
                                $this->friends_list_add();
                        break;
                        case 'remove':
                                $this->friends_list_remove();
                        break;
                        case 'cancel':
                                $this->friends_list_cancel();
                        break;
                        case 'accept':
                                $this->friends_list_accept();
                        break;
                        case 'reject':
                                $this->friends_list_reject();
                        break;

                        case 'friends_list_moderation':
                                $this->friends_list_moderation();
                        break;
                    }
                    break;
                case 'personal_staus_save':
                            $this->personal_staus_save();
                    break;	
    		default: 
                     $this->ipsclass->pageTitle = $this->ipsclass->lang['page_personal_info'];
                    $this->personal_portal_view();
                    break;
    	}
        //echo $this->output;exit();
        $this->output .=  $this->common->load_right_content();
    	$this->ipsclass->print->add_output( $this->output );
        $this->ipsclass->print->do_output( array( 'TITLE' => $this->page_title, 'JS' => 1, 'NAV' => '' ) );
 	}
 	
    /*-------------------------------------------------------------------------*/
    // FRIENDS LIST: Reject a friend request by BabyWolf
    /*-------------------------------------------------------------------------*/
    function personal_staus_save(){
        //-----------------------------------------
        // INIT
        //-----------------------------------------

        $member_id 		 = intval( $this->ipsclass->input['member_id'] );
        $md5check  		 = substr( $this->ipsclass->input['md5check'], 0, 32 );
        $content   		 = '';
        $comment_ids     = array();
        $final_ids       = '';

        //-----------------------------------------
        // MD5 check
        //-----------------------------------------

        if (  $md5check != $this->ipsclass->return_md5_check() )
        {
            print '';
                exit();
        }

        //-----------------------------------------
        // My tab?
        //-----------------------------------------

        if (  ( $member_id != $this->ipsclass->member['id'] ) AND ( ! $this->ipsclass->member['g_is_supmod'] ) )
        {
            print '';
            exit();
        }

        //-----------------------------------------
        // Load member
        //-----------------------------------------

        $member = $this->personal_function_load_member( $member_id );

        //-----------------------------------------
        // Check
        //-----------------------------------------

        if ( ! $member['id'] )
        {
            print '';
            exit();
        }

        //-----------------------------------------
        // Update the database...
        //-----------------------------------------

        $this->ipsclass->DB->do_update( 'profile_portal', array( 'pp_bio_content' => $this->ipsclass->input['bio_content'] ), 'pp_member_id ='.$member_id );
        $this->ipsclass->DB->do_insert( 'profile_comments', array( 'comment_for_member_id' => $member_id,														   'comment_content'	   => $this->ipsclass->my_nl2br( $this->ipsclass->input['bio_content'] ) ) );
        //-----------------------------------------
        // Bounce...
        //-----------------------------------------
        //$this->ipsclass->boink_it("$this->ipsclass->vars['profile_url']/trang-chu.html");      
        $this->ipsclass->boink_it( $this->ipsclass->base_url . 'act=profile&member_id='.$member_id.'&CODE=personal_iframe_comments&_saved=1&___msg=pp_comments_updated&md5check='.$this->ipsclass->md5_check );
    }

    /**
    * Moderate pending friends
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-09
    */
    function friends_list_reject()
    {
            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $friend_id 		  = intval( $this->ipsclass->input['member_id'] );
            $md5check  		  = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $tab        	  = substr( $this->ipsclass->txt_alphanumerical_clean( $this->ipsclass->input['tab'] ), 0, 20 );
            $friend    		  = array();
            $member    		  = array();

            //-----------------------------------------
            // Get the emailer module
            //-----------------------------------------

            require_once( ROOT_PATH."sources/classes/class_email.php" );

            $email           =  new emailer();
            $email->ipsclass =& $this->ipsclass;
            $email->email_init();

            //-----------------------------------------
            // Get MSG library class
            //-----------------------------------------

            require_once( ROOT_PATH.'sources/lib/func_msg.php' );

            $msg_lib           =  new func_msg();
            $msg_lib->ipsclass =& $this->ipsclass;
            $msg_lib->init();
            $this->ipsclass->load_language( 'lang_msg' );

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
            {
            @header( "Content-type: text/plain" );
                $class_ajax->print_nocache_headers();
                print $this->ipsclass->lang['pp_error_md5_check'];
                exit();
            }

            //-----------------------------------------
            // Get friend...
            //-----------------------------------------

            $friend = $this->personal_function_load_member( $friend_id );

            //-----------------------------------------
            // Get member...
            //-----------------------------------------

            $member = $this->personal_function_load_member( $this->ipsclass->member['id'] );

            //-----------------------------------------
            // Check...
            //-----------------------------------------

            if ( ! $friend['id'] OR ! $member['id'] )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['friend_accept_error_loading_info'];
                    exit();
            }

            $friend_check = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'friends_id',
                                                                                                                                              'from'   => 'profile_friends',
                                                                                                                                              'where'  => "friends_friend_id=".$this->ipsclass->member['id']." AND friends_member_id=".$friend['id'] ) );

            if ( ! $friend_check['friends_id'] )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['friend_check_error'];
                    exit();
            }

            //-----------------------------------------
            // Remove from the DB
            //-----------------------------------------

            $this->ipsclass->DB->build_and_exec_query( array( 	'delete' => 'profile_friends',
                                                                    'where'  => 'friends_id='.$friend_check['friends_id'] ) );

            //-----------------------------------------
            // Send out message...
            //-----------------------------------------

            $message = '';
            $subject = '';

            if ( $friend['pp_setting_notify_friend'] )
            {
                    $email->get_template("reject_friend_request");

                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $friend['members_display_name'],
                                                                              'FRIEND_NAME'          => $member['members_display_name'],
                                                                              'LINK'				 => $this->ipsclass->vars['my_url'] . '/index.php?act=UserCP&CODE=manage_friends' ) );

                    $message    = $email->message;
                    $subject    = $email->lang_subject;
                    $return_msg = '';
            }

            //-----------------------------------------
            // Got anything to send?
            //-----------------------------------------

            if ( $message AND $subject )
            {
                    //-----------------------------------------
                    // Email?
                    //-----------------------------------------

                    if ( $friend['pp_setting_notify_friend'] == 'email' OR $friend['members_disable_pm'] )
                    {
                            $email->subject = $subject;
                            $email->message = $message;
                            $email->to      = $friend['email'];

                            $email->send_mail();
                    }
                    //-----------------------------------------
                    // PM?
                    //-----------------------------------------
                    else
                    {
                            $msg_lib->to_by_id    = $friend['id'];
                            $msg_lib->from_member = $member;
                            $msg_lib->msg_title   = $subject;

                            $msg_lib->postlib->parser->parse_bbcode 	= 1;
                            $msg_lib->postlib->parser->parse_smilies 	= 0;
                            $msg_lib->postlib->parser->parse_html 		= 0;
                            $msg_lib->postlib->parser->parse_nl2br 		= 1;
                            $msg_lib->msg_post    = $msg_lib->postlib->parser->pre_display_parse( $msg_lib->postlib->parser->pre_db_parse( $message ) );

                            $msg_lib->force_pm    = 1;

                            $msg_lib->send_pm();
                    }

                    $this->personal_function_recache_members_friends( $friend );
            }

            //-----------------------------------------
            // Recache..
            //-----------------------------------------

            $this->personal_function_recache_members_friends( $member );


            //-----------------------------------------
            // Return
            //-----------------------------------------
            //-----------------------------------------
            // Bounce
            //-----------------------------------------
            @header( "Content-type: text/plain" );
            $class_ajax->print_nocache_headers();
            print $this->ipsclass->lang['m_friend_reject_success'];
            exit();
            //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg='.$msg.'&tab=pending' );
    }

    /*-------------------------------------------------------------------------*/
    // FRIENDS LIST: Accept a friend request by Babywolf
    /*-------------------------------------------------------------------------*/

    /**
    * Moderate pending friends
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-09
    */
    function friends_list_accept()
    {
            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $friend_id 		  = intval( $this->ipsclass->input['member_id'] );
            $md5check  		  = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $tab        	  = substr( $this->ipsclass->txt_alphanumerical_clean( $this->ipsclass->input['tab'] ), 0, 20 );
            $friend    		  = array();
            $member    		  = array();

            //-----------------------------------------
    // Get the emailer module
            //-----------------------------------------

            require_once( ROOT_PATH."sources/classes/class_email.php" );

            $email           =  new emailer();
            $email->ipsclass =& $this->ipsclass;
            $email->email_init();

            //-----------------------------------------
            // Get MSG library class
            //-----------------------------------------

            require_once( ROOT_PATH.'sources/lib/func_msg.php' );

            $msg_lib           =  new func_msg();
            $msg_lib->ipsclass =& $this->ipsclass;
            $msg_lib->init();
            $this->ipsclass->load_language( 'lang_msg' );

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['pp_error_md5_check'];
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab='.$tab );
                    exit();
    }

            //-----------------------------------------
            // Get friend...
            //-----------------------------------------

            $friend = $this->personal_function_load_member( $friend_id );

            //-----------------------------------------
            // Get member...
            //-----------------------------------------

            $member = $this->personal_function_load_member( $this->ipsclass->member['id'] );

            //-----------------------------------------
            // Check...
            //-----------------------------------------

            if ( ! $friend['id'] OR ! $member['id'] )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['friend_accept_error_loading_info'];
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab='.$tab );
                    exit();
            }

            $friend_check = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'friends_id',
                                                                                                                                              'from'   => 'profile_friends',
                                                                                                                                              'where'  => "friends_friend_id=".$this->ipsclass->member['id']." AND friends_member_id=".$friend['id'] ) );

            if ( ! $friend_check['friends_id'] )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['friend_check_error'];
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab='.$tab );
                    exit();
            }

            //-----------------------------------------
            // Ok.. approve them in the DB.
            //-----------------------------------------
            $this->ipsclass->DB->do_update( 'profile_friends', array( 'friends_approved' => 1 ), 'friends_id='.$friend_check['friends_id']);

            // Ok... insert this friend to current user database
            $this->ipsclass->DB->do_insert( 'profile_friends', array( 'friends_member_id' => $member['id'],
                                                                                                                              'friends_friend_id' => $friend['id'],
                                                                                                                              'friends_approved'  => 1,
                                                                                                                              'friends_added'     => time() ) );

            //-----------------------------------------
            // Send out message...
            //-----------------------------------------

            $message = '';
            $subject = '';

            if ( $friend['pp_setting_notify_friend'] )
            {
                    $email->get_template("new_friend_approved");

                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $friend['members_display_name'],
                                                                              'FRIEND_NAME'          => $member['members_display_name'],
                                                                              'LINK'				 => $this->ipsclass->vars['my_url'] . '/index.php?act=UserCP&CODE=manage_friends' ) );

                    $message    = $email->message;
                    $subject    = $email->lang_subject;
                    $return_msg = '';
            }

            //-----------------------------------------
            // Got anything to send?
            //-----------------------------------------

            if ( $message AND $subject )
            {
                    //-----------------------------------------
                    // Email?
                    //-----------------------------------------

                    if ( $friend['pp_setting_notify_friend'] == 'email' OR $friend['members_disable_pm'] )
                    {
                            $email->subject = $subject;
                            $email->message = $message;
                            $email->to      = $friend['email'];

                            $email->send_mail();
                    }
                    //-----------------------------------------
                    // PM?
                    //-----------------------------------------
                    else
                    {
                            $msg_lib->to_by_id    = $friend['id'];
                            $msg_lib->from_member = $member;
                            $msg_lib->msg_title   = $subject;

                            $msg_lib->postlib->parser->parse_bbcode 	= 1;
                            $msg_lib->postlib->parser->parse_smilies 	= 0;
                            $msg_lib->postlib->parser->parse_html 		= 0;
                            $msg_lib->postlib->parser->parse_nl2br 		= 1;
                            $msg_lib->msg_post    = $msg_lib->postlib->parser->pre_display_parse( $msg_lib->postlib->parser->pre_db_parse( $message ) );

                            $msg_lib->force_pm    = 1;

                            $msg_lib->send_pm();
                    }

                    $this->personal_function_recache_members_friends( $friend );
            }

            //-----------------------------------------
            // Recache..
            //-----------------------------------------

            $this->personal_function_recache_members_friends( $member );


            //-----------------------------------------
            // Return
            //-----------------------------------------
            //-----------------------------------------
            // Bounce
            //-----------------------------------------
            @header( "Content-type: text/plain" );
            $class_ajax->print_nocache_headers();
            print $this->ipsclass->lang['m_friend_accept_success'];
            exit();
            //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg='.$msg.'&tab=pending' );
    }

    /*-------------------------------------------------------------------------*/
    // FRIENDS LIST: Cancel request friend that current user have been added by BabyWolf
    /*-------------------------------------------------------------------------*/

    /**
    * Remove a friend
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-09
    */
    function friends_list_cancel()
    {
            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $friend_id 		  = intval( $this->ipsclass->input['member_id'] );
            $md5check  		  = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $tab        	  = substr( $this->ipsclass->txt_alphanumerical_clean( $this->ipsclass->input['tab'] ), 0, 20 );
            $friend    		  = array();
            $member    		  = array();

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['pp_error_md5_check'];
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab='.$tab );
                    exit();
    }

            //-----------------------------------------
            // Get friend...
            //-----------------------------------------

            $friend = $this->personal_function_load_member( $friend_id );

            //-----------------------------------------
            // Get member...
            //-----------------------------------------

            $member = $this->personal_function_load_member( $this->ipsclass->member['id'] );

            //-----------------------------------------
            // Check...
            //-----------------------------------------

            if ( ! $friend['id'] OR ! $member['id'] )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['friend_cancel_error_loading_info'];
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab='.$tab );
                    exit();
            }

            $friend_check = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'friends_id',
                                                                                                                                              'from'   => 'profile_friends',
                                                                                                                                              'where'  => "friends_member_id=".$this->ipsclass->member['id']." AND friends_friend_id=".$friend['id'] ) );

            if ( ! $friend_check['friends_id'] )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print "error";
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab='.$tab );
                    exit();
            }

            //-----------------------------------------
            // Remove from the DB
            //-----------------------------------------

            $this->ipsclass->DB->build_and_exec_query( array( 	'delete' => 'profile_friends',
                                                                                                                    'where'  => 'friends_id='.$friend_check['friends_id'] ) );


            //-----------------------------------------
            // Send out message...
            //-----------------------------------------

            $message = '';
            $subject = '';

            if ( $friend['pp_setting_notify_friend'] )
            {
                    $email->get_template("cancel_friend_request");

                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $friend['members_display_name'],
                                                                              'FRIEND_NAME'          => $member['members_display_name'],
                                                                              'LINK'				 => $this->ipsclass->vars['my_url'] . '/index.php?act=UserCP&CODE=manage_friends' ) );

                    $message    = $email->message;
                    $subject    = $email->lang_subject;
                    $return_msg = '';
            }

            //-----------------------------------------
            // Got anything to send?
            //-----------------------------------------

            if ( $message AND $subject )
            {
                    //-----------------------------------------
                    // Email?
                    //-----------------------------------------

                    if ( $friend['pp_setting_notify_friend'] == 'email' OR $friend['members_disable_pm'] )
                    {
                            $email->subject = $subject;
                            $email->message = $message;
                            $email->to      = $friend['email'];

                            $email->send_mail();
                    }
                    //-----------------------------------------
                    // PM?
                    //-----------------------------------------
                    else
                    {
                            $msg_lib->to_by_id    = $friend['id'];
                            $msg_lib->from_member = $member;
                            $msg_lib->msg_title   = $subject;

                            $msg_lib->postlib->parser->parse_bbcode 	= 1;
                            $msg_lib->postlib->parser->parse_smilies 	= 0;
                            $msg_lib->postlib->parser->parse_html 		= 0;
                            $msg_lib->postlib->parser->parse_nl2br 		= 1;
                            $msg_lib->msg_post    = $msg_lib->postlib->parser->pre_display_parse( $msg_lib->postlib->parser->pre_db_parse( $message ) );

                            $msg_lib->force_pm    = 1;

                            $msg_lib->send_pm();
                    }

                    $this->personal_function_recache_members_friends( $friend );
            }

            //-----------------------------------------
            // Recache..
            //-----------------------------------------

            $this->personal_function_recache_members_friends( $member );
            $this->personal_function_recache_members_friends( $friend );

            //-----------------------------------------
            // Bounce
            //-----------------------------------------
            @header( "Content-type: text/plain" );
            $class_ajax->print_nocache_headers();
            print $this->ipsclass->lang['m_friend_cancel_success'];
            exit();

            //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=pp_friend_removed&tab='.$tab );
    }

    /*-------------------------------------------------------------------------*/
    // FRIENDS LIST: Remove a friend
    /*-------------------------------------------------------------------------*/

    /**
    * Remove a friend
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-09
    */
    function friends_list_remove()
    {
            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $friend_id 		  = intval( $this->ipsclass->input['member_id'] );
            $md5check  		  = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $tab        	  = substr( $this->ipsclass->txt_alphanumerical_clean( $this->ipsclass->input['tab'] ), 0, 20 );
            $friend    		  = array();
            $member    		  = array();

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['pp_error_md5_check'];
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab='.$tab );
                    exit();
    }

            //-----------------------------------------
            // Get friend...
            //-----------------------------------------

            $friend = $this->personal_function_load_member( $friend_id );

            //-----------------------------------------
            // Get member...
            //-----------------------------------------

            $member = $this->personal_function_load_member( $this->ipsclass->member['id'] );

            //-----------------------------------------
            // Check...
            //-----------------------------------------

            if ( ! $friend['id'] OR ! $member['id'] )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['friend_remove_error_loading_info'];
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab='.$tab );
                    exit();
            }

            //-----------------------------------------
            // Already a friend?
            //-----------------------------------------

            $friend_check = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'friends_id',
                                                                                                                                              'from'   => 'profile_friends',
                                                                                                                                              'where'  => "friends_approved=1 AND friends_member_id=".$this->ipsclass->member['id']." AND friends_friend_id=".$friend['id'] ) );

            if ( ! $friend_check['friends_id'] )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['friend_check_error'];
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab='.$tab );
                    exit();
            }

            //-----------------------------------------
            // Remove from the DB
            //-----------------------------------------

            $this->ipsclass->DB->build_and_exec_query( array( 'delete' => 'profile_friends',
                                                                                                              'where'  => 'friends_id='.$friend_check['friends_id'] ) );

            $this->ipsclass->DB->build_and_exec_query( array( 'delete' => 'profile_friends',
                                                                                                              'where'  => 'friends_member_id='.$this->ipsclass->member['id'].' AND friends_friend_id='.$friend['id'] ) );


            //-----------------------------------------
            // Recache..
            //-----------------------------------------

            $this->personal_function_recache_members_friends( $member );
            $this->personal_function_recache_members_friends( $friend );

            //-----------------------------------------
            // Send out message...
            //-----------------------------------------

            //-----------------------------------------
    // Get the emailer module
            //-----------------------------------------

            require_once( ROOT_PATH."sources/classes/class_email.php" );

            $email           =  new emailer();
            $email->ipsclass =& $this->ipsclass;
            $email->email_init();

            //-----------------------------------------
            // Get MSG library class
            //-----------------------------------------

            require_once( ROOT_PATH.'sources/lib/func_msg.php' );

            $msg_lib           =  new func_msg();
            $msg_lib->ipsclass =& $this->ipsclass;
            $msg_lib->init();
            $this->ipsclass->load_language( 'lang_msg' );

            $message = '';
            $subject = '';

            if ( $friend['pp_setting_notify_friend'] )
            {
                    $email->get_template("friend_removed");

                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $friend['members_display_name'],
                                                                              'FRIEND_NAME'          => $member['members_display_name'],
                                                                              'LINK'				 => $this->ipsclass->vars['my_url'] . '/index.php?act=UserCP&CODE=manage_friends' ) );

                    $message    = $email->message;
                    $subject    = $email->lang_subject;
                    $return_msg = '';
            }

            //-----------------------------------------
            // Got anything to send?
            //-----------------------------------------

            if ( $message AND $subject )
            {
                    //-----------------------------------------
                    // Email?
                    //-----------------------------------------

                    if ( $friend['pp_setting_notify_friend'] == 'email' OR $friend['members_disable_pm'] )
                    {
                            $email->subject = $subject;
                            $email->message = $message;
                            $email->to      = $friend['email'];

                            $email->send_mail();
                    }
                    //-----------------------------------------
                    // PM?
                    //-----------------------------------------
                    else
                    {
                            $msg_lib->to_by_id    = $friend['id'];
                            $msg_lib->from_member = $member;
                            $msg_lib->msg_title   = $subject;

                            $msg_lib->postlib->parser->parse_bbcode 	= 1;
                            $msg_lib->postlib->parser->parse_smilies 	= 0;
                            $msg_lib->postlib->parser->parse_html 		= 0;
                            $msg_lib->postlib->parser->parse_nl2br 		= 1;
                            $msg_lib->msg_post    = $msg_lib->postlib->parser->pre_display_parse( $msg_lib->postlib->parser->pre_db_parse( $message ) );

                            $msg_lib->force_pm    = 1;

                            $msg_lib->send_pm();
                    }

                    $this->personal_function_recache_members_friends( $friend );
            }

            //-----------------------------------------
            // Bounce
            //-----------------------------------------
            @header( "Content-type: text/plain" );
            $class_ajax->print_nocache_headers();
            print $this->ipsclass->lang['m_friend_remove_success'];
            exit();

            //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=pp_friend_removed&tab='.$tab );
    }

    /*-------------------------------------------------------------------------*/
    // FRIENDS LIST: Approve a friend
    /*-------------------------------------------------------------------------*/

    /**
    * Moderate pending friends
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-09
    */
    function friends_list_moderation()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $md5check  		   = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $friends    	   = array();
            $friend_ids        = array();
            $friend_member_ids = array();
            $_friend_ids       = array();
            $friends_already   = array();
            $friends_update    = array();
            $member    		   = array();
            $pp_option         = trim( $this->ipsclass->input['pp_option'] );
            $message		   = '';
            $subject		   = '';
            $msg               = 'pp_friend_approved';

            //-----------------------------------------
    // Get the emailer module
            //-----------------------------------------

            require_once( ROOT_PATH."sources/classes/class_email.php" );

            $email           =  new emailer();
            $email->ipsclass =& $this->ipsclass;
            $email->email_init();

            //-----------------------------------------
            // Get MSG library class
            //-----------------------------------------

            require_once( ROOT_PATH.'sources/lib/func_msg.php' );

            $msg_lib           =  new func_msg();
            $msg_lib->ipsclass =& $this->ipsclass;
            $msg_lib->init();
            $this->ipsclass->load_language( 'lang_msg' );

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            $this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab=pending' );
                    exit();
    }

            //-----------------------------------------
            // Get friends...
            //-----------------------------------------

            if ( ! is_array( $_POST['pp_friend_id'] ) OR ! count( $_POST['pp_friend_id'] ) )
            {
                    $this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab=pending' );
                    exit();
            }

            //-----------------------------------------
            // Figure IDs
            //-----------------------------------------

            foreach( $_POST['pp_friend_id'] as $key => $value )
            {
                    $_key = intval( $key );

                    if ( $_key )
                    {
                            $_friend_ids[ $_key ] = $_key;
                    }
            }

            if ( ! is_array( $_friend_ids ) OR ! count( $_friend_ids ) )
            {
                    $this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab=pending' );
                    exit();
            }

            //-----------------------------------------
            // Check our friends are OK
            //-----------------------------------------

            $this->ipsclass->DB->build_query( array( 'select' => '*',
                                                                                             'from'   => 'profile_friends',
                                                                                             'where'  => 'friends_friend_id='.$this->ipsclass->member['id'].' AND friends_approved=0 AND friends_member_id IN ('.implode(',',$_friend_ids ). ')' ) );

            $this->ipsclass->DB->exec_query();

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                    $friend_ids[ $row['friends_id'] ]               = $row['friends_id'];
                    $friend_member_ids[ $row['friends_member_id'] ] = $row['friends_member_id'];
            }

            if ( ! is_array( $friend_ids ) OR ! count( $friend_ids ) )
            {
                    $this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab=pending' );
                    exit();
            }

            //-----------------------------------------
            // Load friends...
            //-----------------------------------------

            $friends = $this->personal_function_load_member( $friend_member_ids );

            //-----------------------------------------
            // Get member...
            //-----------------------------------------

            $member = $this->personal_function_load_member( $this->ipsclass->member['id'] );

            //-----------------------------------------
            // Check...
            //-----------------------------------------

            if ( ! is_array( $friends ) OR ! count( $friends ) OR ! $member['id'] )
            {
                    $this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error&tab=pending' );
                    exit();
            }

            //-----------------------------------------
            // What to do?
            //-----------------------------------------

            if ( $pp_option == 'delete' )
            {
                    //-----------------------------------------
                    // Ok.. delete them in the DB.
                    //-----------------------------------------

                    $this->ipsclass->DB->do_delete( 'profile_friends', 'friends_id IN('.implode(',', $friend_ids ) . ')' );

                    $msg = 'pp_friend_removed';
            }
            else
            {
                    //-----------------------------------------
                    // Ok.. approve them in the DB.
                    //-----------------------------------------

                    $this->ipsclass->DB->do_update( 'profile_friends', array( 'friends_approved' => 1 ), 'friends_id IN('.implode(',', $friend_ids ) . ')' );

                    //-----------------------------------------
                    // Reciprocal mode?
                    //-----------------------------------------

                    if ( $pp_option == 'add_reciprocal' )
                    {
                            //-----------------------------------------
                            // Find out who isn't already on your list...
                            //-----------------------------------------

                            $this->ipsclass->DB->build_query( array( 'select' => '*',
                                                                                                             'from'   => 'profile_friends',
                                                                                                             'where'  => 'friends_member_id='.$this->ipsclass->member['id'].' AND friends_approved=1 AND friends_friend_id IN ('.implode(',',$_friend_ids ). ')' ) );

                            $this->ipsclass->DB->exec_query();

                            while( $row = $this->ipsclass->DB->fetch_row() )
                            {
                                    $friends_already[ $row['friends_friend_id'] ] = $row['friends_friend_id'];
                            }

                            //-----------------------------------------
                            // Check which aren't already members...	
                            //-----------------------------------------

                            foreach( $friend_member_ids as $id => $_id )
                            {
                                    if ( in_array( $id, $friends_already ) )
                                    {
                                            continue;
                                    }

                                    $friends_update[ $id ] = $id;
                            }

                            //-----------------------------------------
                            // Gonna do it?
                            //-----------------------------------------

                            if ( is_array( $friends_update ) AND count( $friends_update ) )
                            {
                                    foreach( $friends_update as $id => $_id )
                                    {
                                            $this->ipsclass->DB->do_insert( 'profile_friends', array( 'friends_member_id' => $member['id'],
                                                                                                                                                              'friends_friend_id' => $id,
                                                                                                                                                              'friends_approved'  => 1,
                                                                                                                                                              'friends_added'     => time() ) );
                                    }
                            }
                    }

                    //-----------------------------------------
                    // Send out message...
                    //-----------------------------------------

                    foreach( $friends as $friend )
                    {
                            //-----------------------------------------
                            // INIT
                            //-----------------------------------------

                            $message = '';
                            $subject = '';

                            if ( $friend['pp_setting_notify_friend'] )
                            {
                                    $email->get_template("new_friend_approved");

                                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $friend['members_display_name'],
                                                                                              'FRIEND_NAME'          => $member['members_display_name'],
                                                                                              'LINK'				 => $this->ipsclass->vars['board_url'] . '/index.' . $this->ipsclass->vars['php_ext'] . '?act=profile&CODE=personal_portal_view&tab=settings&id='.$friend['id'] ) );

                                    $message    = $email->message;
                                    $subject    = $email->lang_subject;
                                    $return_msg = '';
                            }

                            //-----------------------------------------
                            // Got anything to send?
                            //-----------------------------------------

                            if ( $message AND $subject )
                            {
                                    //-----------------------------------------
                                    // Email?
                                    //-----------------------------------------

                                    if ( $friend['pp_setting_notify_friend'] == 'email' OR $friend['members_disable_pm'] )
                                    {
                                            $email->subject = $subject;
                                            $email->message = $message;
                                            $email->to      = $friend['email'];

                                            $email->send_mail();
                                    }

                                    //-----------------------------------------
                                    // PM?
                                    //-----------------------------------------

                                    else
                                    {
                                            $msg_lib->to_by_id    = $friend['id'];
                                            $msg_lib->from_member = $member;
                                            $msg_lib->msg_title   = $subject;

                                            $msg_lib->postlib->parser->parse_bbcode 	= 1;
                                            $msg_lib->postlib->parser->parse_smilies 	= 0;
                                            $msg_lib->postlib->parser->parse_html 		= 0;
                                            $msg_lib->postlib->parser->parse_nl2br 		= 1;
                                            $msg_lib->msg_post    = $msg_lib->postlib->parser->pre_display_parse( $msg_lib->postlib->parser->pre_db_parse( $message ) );

                                            $msg_lib->force_pm    = 1;

                                            $msg_lib->send_pm();
                                    }
                            }
                    }

                    $this->personal_function_recache_members_friends( $friend );
            }

            //-----------------------------------------
            // Recache..
            //-----------------------------------------

            $this->personal_function_recache_members_friends( $member );


            //-----------------------------------------
            // Return
            //-----------------------------------------

            $this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg='.$msg.'&tab=pending' );
    }

    /*-------------------------------------------------------------------------*/
    // FRIENDS LIST: Add a friend
    /*-------------------------------------------------------------------------*/

    /**
    * Add a friend
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-09
    */
    function friends_list_add()
    {
            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $friend_id 		  = intval( $this->ipsclass->input['member_id'] );
            $md5check  		  = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $friend    		  = array();
            $member    		  = array();
            $friends_approved = 1;
            $message		  = '';
            $subject		  = '';
            $to               = array();
            $from             = array();
            $return_msg       = '';

            //-----------------------------------------
    // Get the emailer module
            //-----------------------------------------

            require_once( ROOT_PATH."sources/classes/class_email.php" );

            $email           =  new emailer();
            $email->ipsclass =& $this->ipsclass;
            $email->email_init();

            //-----------------------------------------
            // Get MSG library class
            //-----------------------------------------

            require_once( ROOT_PATH.'sources/lib/func_msg.php' );

            $msg_lib           =  new func_msg();
            $msg_lib->ipsclass =& $this->ipsclass;
            $msg_lib->init();
            $this->ipsclass->load_language( 'lang_msg' );

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {

            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
            //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error' );
                    exit();
    }

            //-----------------------------------------
            // Adding yourself?
            //-----------------------------------------

            if ( $this->ipsclass->member['id'] == $friend_id )
    {

            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
            //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error' );
                    exit();
    }

            //-----------------------------------------
            // Get friend...
            //-----------------------------------------

            $friend = $this->personal_function_load_member( $friend_id );

            //-----------------------------------------
            // Get member...
            //-----------------------------------------

            $member = $this->personal_function_load_member( $this->ipsclass->member['id'] );

            //-----------------------------------------
            // Check...
            //-----------------------------------------

            if ( ! $friend['id'] OR ! $member['id'] )
            {

            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=error' );
                    exit();
            }

            //-----------------------------------------
            // Already a friend?
            //-----------------------------------------

            $friend_check = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'friends_id',
                                                                                                                                              'from'   => 'profile_friends',
                                                                                                                                              'where'  => "friends_member_id=".$this->ipsclass->member['id']." AND friends_friend_id=".$friend['id'] ) );

            if ( $friend_check['friends_id'] )
            {

            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print $this->ipsclass->lang['pp_friend_already'];
                    //$this->ipsclass->boink_it( $this->ipsclass->base_url . '&act=profile&CODE=friends_list_popup&___msg=pp_friend_already' );
                    exit();
            }

            //-----------------------------------------
            // Friend requires approval?
            //-----------------------------------------
            // Set default as is moderate by BabyWolf
            $friend['pp_setting_moderate_friends'] = isset($friend['pp_setting_moderate_friends'])?$friend['pp_setting_moderate_friends']:1;

            if ( $friend['pp_setting_moderate_friends'] )
            {
                    $friends_approved = 0;
            }

            //-----------------------------------------
            // Add to DB
            //-----------------------------------------

            $this->ipsclass->DB->do_insert( 'profile_friends', array( 'friends_member_id' => $member['id'],
                                                                                                                              'friends_friend_id' => $friend['id'],
                                                                                                                              'friends_approved'  => $friends_approved,
                                                                                                                              'friends_added'     => time() ) );

            //-----------------------------------------
            // What are we doing? Sending out 'mod' or 'yay'
            // message?
            //-----------------------------------------

            if ( ! $friends_approved AND $friend['pp_setting_notify_friend'] )
            {
                    $email->get_template("new_friend_request");

                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $friend['members_display_name'],
                                                                              'FRIEND_NAME'          => $member['members_display_name'],
                                                                              'LINK'				 => $this->ipsclass->vars['my_url'] . '/index.php?act=UserCP&CODE=manage_friends',
                                                                              'MSG' 				 => $this->ipsclass->input['msg']
                                                                                    ) );

                    $message    = $email->message;
                    $subject    = $email->lang_subject;
                    $to         = $friend;
                    $from       = $member;
                    $return_msg = 'pp_friend_added_mod';
            }
            else if ( $friend['pp_setting_notify_friend'] != 'none' )
            {
                    $email->get_template("new_friend_added");

                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $friend['members_display_name'],
                                                                              'FRIEND_NAME'          => $member['members_display_name'],
                                                                              'LINK'				 => $this->ipsclass->vars['profile_url'] . '/index.php?act=UserCP&CODE=manage_friends',
                                                                              'MSG' 				 => $this->ipsclass->input['msg']
                                                                                    ) );

                    $message    = $email->message;
                    $subject    = $email->lang_subject;
                    $to         = $friend;
                    $from       = $member;
                    $return_msg = 'pp_friend_added';
            }

            //-----------------------------------------
            // Got anything to send?
            //-----------------------------------------

            if ( $message AND $subject )
            {
                    //-----------------------------------------
                    // Email?
                    //-----------------------------------------

                    if ( $friend['pp_setting_notify_friend'] == 'email' OR ( $friend['pp_setting_notify_friend'] AND $friend['members_disable_pm'] ) )
                    {
                            $email->subject = $subject;
                            $email->message = $message;
                            $email->to      = $to['email'];

                            $email->send_mail();
                    }

                    //-----------------------------------------
                    // PM?
                    //-----------------------------------------

                    else if ( $friend['pp_setting_notify_friend'] )
                    {
                            $msg_lib->to_by_id    = $to['id'];
                            $msg_lib->from_member = $from;
                            $msg_lib->msg_title   = $subject;

                            $msg_lib->postlib->parser->parse_bbcode 	= 1;
                            $msg_lib->postlib->parser->parse_smilies 	= 0;
                            $msg_lib->postlib->parser->parse_html 		= 0;
                            $msg_lib->postlib->parser->parse_nl2br 		= 1;
                            $msg_lib->msg_post    = $msg_lib->postlib->parser->pre_display_parse( $msg_lib->postlib->parser->pre_db_parse( $message ) );

                            $msg_lib->force_pm    = 1;

                            $msg_lib->send_pm();
                    }
            }

            //-----------------------------------------
            // Recache..
            //-----------------------------------------

            $this->personal_function_recache_members_friends( $member );
            $this->personal_function_recache_members_friends( $friend );

            //-----------------------------------------
            // Return
            //-----------------------------------------
            @header( "Content-type: text/plain" );
            $class_ajax->print_nocache_headers();
            print $this->ipsclass->lang[$return_msg];
            exit();
            //$this->ipsclass->boink_it( $this->ipsclass->profile_url . '&act=profile&CODE=friends_list_popup&___msg='.$return_msg );
    }

    /*-------------------------------------------------------------------------*/
    // FRIENDS LIST: List all friends
    /*-------------------------------------------------------------------------*/

    /**
    * List all current friends.
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-08
    */
    function friends_list_list()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $content    	= '';
            $member_id  	= intval( $this->ipsclass->member['id'] );
            $friends    	= array();
            $tab        	= substr( $this->ipsclass->txt_alphanumerical_clean( $this->ipsclass->input['tab'] ), 0, 20 );
            $friends_filter = substr( $this->ipsclass->txt_alphanumerical_clean( $this->ipsclass->input['friends_filter'] ), 0, 20 );
            $_mutual_ids    = array( 0 => 0 );
            $query      	= '';
            $join_field	    = '';
            $time_limit     = time() - $this->ipsclass->vars['au_cutoff'] * 60;
            $per_page       = 25;
            $start          = intval( $this->ipsclass->input['st'] );

            //-----------------------------------------
            // Check we're a member
            //-----------------------------------------

            if ( ! $member_id )
            {
                    print '';
                    exit();
            }

            //-----------------------------------------
            // To what are we doing to whom?
            //-----------------------------------------

            if ( $tab == 'pending' )
            {
                    $query      = 'f.friends_friend_id='.$member_id.' AND f.friends_approved=0';
                    $join_field = 'f.friends_member_id';
            }
            else if ( $tab == 'mutual' AND $friends_filter == 'added' )
            {
                    $query      = 'f.friends_friend_id='.$member_id.' AND f.friends_approved=1';
                    $join_field = 'f.friends_member_id';
            }
            else if ( $tab == 'mutual' )
            {
                    # My friends...
                    $this->ipsclass->DB->build_query( array( 'select' => '*',
                                                                                                     'from'   => 'profile_friends',
                                                                                                     'where'  => 'friends_member_id='.$member_id.' AND friends_approved=1' ) );

                    $this->ipsclass->DB->exec_query();

                    while( $row = $this->ipsclass->DB->fetch_row() )
                    {
                            $_mutual_ids[] = $row['friends_friend_id'];
                    }

                    $query      = 'f.friends_member_id IN ('.implode(',', $_mutual_ids).') AND f.friends_friend_id='.$member_id.' AND f.friends_approved=1';
                    $join_field = 'f.friends_member_id';
            }
            else
            {
                    $query      = 'f.friends_member_id='.$member_id;
                    $join_field = 'f.friends_friend_id';
            }

            //-----------------------------------------
            // Filtered?
            //-----------------------------------------

            if ( $friends_filter == 'online' )
            {
                    $query .= " AND ( ( m.last_visit > $time_limit OR m.last_activity > $time_limit ) AND m.login_anonymous='0&1' )";
            }
            else if ( $friends_filter == 'offline' )
            {
                    $query .= " AND ( m.last_activity < $time_limit OR ( m.login_anonymous='0&0' OR m.login_anonymous='1&0' ) )";
            }

            //-----------------------------------------
            // Get count...
            //-----------------------------------------

            $this->ipsclass->DB->build_query( array( 'select'   => 'COUNT(*) as count',
                                                                                             'from'     => array( 'profile_friends' => 'f' ),
                                                                                             'where'    => $query,
                                                                                             'add_join' => array( 0 => array( 'select' => '',
                                                                                                                                                              'from'   => array( 'members' => 'm' ),
                                                                                                                                                              'where'  => 'm.id='.$join_field,
                                                                                                                                                              'type'   => 'inner' ) ) ) );
            $this->ipsclass->DB->exec_query();

            $count = $this->ipsclass->DB->fetch_row();

            //-----------------------------------------
            // Pages...
            //----------------------------------------- 

            $pages = $this->ipsclass->build_pagelinks( array(  'TOTAL_POSS'  => intval( $count['count'] ),
                                                                                                               'no_dropdown' => 1,
                                                                                                               'PER_PAGE'    => $per_page,
                                                                                                               'CUR_ST_VAL'  => $start,
                                                                                                               'L_SINGLE'    => "",
                                                                                                               'BASE_URL'    => $this->ipsclass->base_url . 'act=profile&member_id='.$member_id.'&CODE=friends_list_popup&tab='.$tab.'&friends_filter='.$friends_filter,
                                                                                                             ) );
            //-----------------------------------------
            // Get current friends...	
            //-----------------------------------------

            $this->ipsclass->DB->build_query( array( 'select'   => 'f.*',
                                                                                             'from'     => array( 'profile_friends' => 'f' ),
                                                                                             'where'    => $query,
                                                                                             'order'    => 'm.members_l_display_name ASC',
                                                                                             'limit'    => array( $start, $per_page ),
                                                                                             'add_join' => array( 0 => array( 'select' => 'me.*',
                                                                                                                                                              'from'   => array( 'member_extra' => 'me' ),
                                                                                                                                                              'where'  => 'me.id='.$join_field,
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      1 => array( 'select' => 'pp.*',
                                                                                                                                                              'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                                                                              'where'  => 'pp.pp_member_id='.$join_field,
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      2 => array( 'select' => 'm.*',
                                                                                                                                                              'from'   => array( 'members' => 'm' ),
                                                                                                                                                              'where'  => 'm.id='.$join_field,
                                                                                                                                                              'type'   => 'left' ) ) ) );
            $this->ipsclass->DB->exec_query();

            //-----------------------------------------
            // Get and store...
            //-----------------------------------------

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                    //-----------------------------------------
                    // Others...
                    //-----------------------------------------

                    $row['_last_active']   = $this->ipsclass->get_date( $row['last_activity'], 'SHORT' );

                    if( $row['login_anonymous']{0} == '1' )
                    {
                            // Member last logged in anonymous

                            if( $this->ipsclass->member['mgroup'] != $this->ipsclass->vars['admin_group'] OR $this->ipsclass->vars['disable_admin_anon'] )
                            {
                                    $row['_last_active'] = $this->ipsclass->lang['private'];
                            }
                    }

                    $row['_friends_added'] = $this->ipsclass->get_date( $row['friends_added'], 'SHORT' );
                    $row['g_title']        = $this->ipsclass->cache['group_cache'][ $row['mgroup'] ]['g_title'];

                    $row = $this->personal_portal_set_information( $row );

                    //-----------------------------------------
                    // Add row...
                    //-----------------------------------------

                    $friends[] = $row;
            }

            //-----------------------------------------
            // Show...
            //-----------------------------------------

            $content = $this->ipsclass->compiled_templates['skin_profile']->friends_list_list( $friends, $pages );

            $this->ipsclass->print->pop_up_window( $this->ipsclass->lang['m_title_friends'], $content );
    }

    /*-------------------------------------------------------------------------*/
    // Recaches member's friends
    /*-------------------------------------------------------------------------*/

    /**
    * Recaches member's friends
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-09
    */
    function personal_function_recache_members_friends( $member )
    {
            //-----------------------------------------
            // INIT	
            //-----------------------------------------

            $friends = array();

            //-----------------------------------------
            // Check
            //-----------------------------------------

            if ( ! $member['id'] )
            {
                    return FALSE;
            }

            //-----------------------------------------
            // Get current friends...	
            //-----------------------------------------

            $this->ipsclass->DB->build_query( array( 'select'   => '*',
                                                                                             'from'     => 'profile_friends',
                                                                                            // 'where'    => 'friends_member_id='.$member['id']
                                                                                            // Updated by BabyWolf, only build cache for approved friend
                                                                                             'where'    => 'friends_member_id='.$member['id'].' AND friends_approved=1' ) );
            $this->ipsclass->DB->exec_query();

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                    $friends[ $row['friends_friend_id'] ] = $row['friends_approved'];
            }

            //-----------------------------------------
            // Update DB
            //-----------------------------------------

            $this->ipsclass->pack_and_update_member_cache( $member['id'], array( 'friends' => $friends ) );

            return TRUE;
    }

    /*-------------------------------------------------------------------------*/
    // Load member...
    /*-------------------------------------------------------------------------*/

    /**
    * Loads the member
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_function_load_member( $member_id=0 )
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $query = '';

            //-----------------------------------------
            // What do we have?
            //-----------------------------------------

            if ( is_array( $member_id ) )
            {
                    $query = 'm.id IN ('.implode( ',', $member_id ) . ')';
            }
            else
            {
                    $query = 'm.id='.intval($member_id);
            }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $this->ipsclass->DB->build_query( array( 'select'   => 'm.*',
                                                                                             'from'     => array( 'members' => 'm' ),
                                                                                             'where'    => $query,
                                                                                             'add_join' => array( 0 => array( 'select' => 'me.*',
                                                                                                                                                              'from'   => array( 'member_extra' => 'me' ),
                                                                                                                                                              'where'  => 'me.id=m.id',
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      1 => array( 'select' => 'pp.*',
                                                                                                                                                              'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                                                                              'where'  => 'pp.pp_member_id=m.id',
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      2 => array( 'select' => 'g.*',
                                                                                                                                                              'from'   => array( 'groups' => 'g' ),
                                                                                                                                                              'where'  => 'g.g_id=m.mgroup',
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      3 => array( 'select' => 's.location_1_id, s.location_2_id, s.location_1_type, s.location_2_type, s.running_time, s.location as sesslocation',
                                                                                                                                                              'from'   => array( 'sessions' => 's' ),
                                                                                                                                                              'where'  => "s.member_id=m.id",
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      4 => array( 'select' => 'pc.*',
                                                                                                                                                              'from'   => array( 'pfields_content' => 'pc' ),
                                                                                                                                                              'where'  => 'pc.member_id=m.id',
                                                                                                                                                              'type'   => 'left' ) ) ) );
            $this->ipsclass->DB->exec_query();

            if ( is_array( $member_id ) )
            {
                    while( $row = $this->ipsclass->DB->fetch_row() )
                    {
                            $member[ $row['id'] ] = $row;
                    }
            }
            else
            {
                    $member = $this->ipsclass->DB->fetch_row();

                    $member['pp_setting_count_visitors'] = ( $member['pp_setting_count_visitors'] != 0 ) ? $member['pp_setting_count_visitors'] : 5;
                    $member['pp_setting_count_comments'] = ( $member['pp_setting_count_comments'] != 0)  ? $member['pp_setting_count_comments'] : 5;
                    $member['pp_setting_count_friends']  = ( $member['pp_setting_count_friends'] != 0 )  ? $member['pp_setting_count_friends']  : 5;
                    $member['joined'] = $this->ipsclass->get_date($member['joined'], 'SHORT');
                    $member['last_visit'] = $this->ipsclass->get_date($member['last_visit'], 'SHORT');
                    //$now =  date("Y m, d, g:i a");
                    $time = time();
                    $member['now'] = date("Y m, d") ." - ".$this->ipsclass->get_date($time, 'SHORT');
                    $member['total_topic'] = $this->count("topics", "tid", " starter_id = " . $member['id']);
                    //echo (time() - $member['joined'])/365/12/30/24/60/60;exit();

                    $member['avg_post'] = substr((time() - $member['joined'])/365/12/30/24/60/60, 0, 4);
            }

            return $member;
    }

    /*-------------------------------------------------------------------------*/
    // Updates comments
    /*-------------------------------------------------------------------------*/

    /**
    * Updates the comments
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-15
    */
    function personal_iframe_comments_save()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id 		 = intval( $this->ipsclass->input['member_id'] );
            $md5check  		 = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $content   		 = '';
            $comment_ids     = array();
            $final_ids       = '';

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            print '';
                    exit();
    }

            //-----------------------------------------
            // My tab?
            //-----------------------------------------

            if (  ( $member_id != $this->ipsclass->member['id'] ) AND ( ! $this->ipsclass->member['g_is_supmod'] ) )
    {
            print '';
                    exit();
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    print '';
                    exit();
    }

            //-----------------------------------------
            // Grab comment_ids
            //-----------------------------------------

            if ( is_array( $_POST['pp-checked'] ) AND count( $_POST['pp-checked'] ) )
            {
                    foreach( $_POST['pp-checked'] as $key => $value )
                    {
                            $key = intval( $key );

                            if ( $value )
                            {
                                    $comment_ids[ $key ] = $key;
                            }
                    }
            }

            //-----------------------------------------
            // Update the database...
            //-----------------------------------------

            if ( is_array( $comment_ids ) AND count( $comment_ids ) )
            {
                    $final_ids = implode( ',', $comment_ids );

                    //-----------------------------------------
                    // Now update...
                    //-----------------------------------------

                    switch( $this->ipsclass->input['pp-moderation'] )
                    {
                            case 'approve':
                                    $this->ipsclass->DB->do_update( 'profile_comments', array( 'comment_approved' => 1 ), 'comment_id IN('.$final_ids.')' );
                                    break;
                            case 'unapprove':
                                    $this->ipsclass->DB->do_update( 'profile_comments', array( 'comment_approved' => 0 ), 'comment_id IN('.$final_ids.')' );
                                    break;
                            case 'delete':
                                    $this->ipsclass->DB->do_delete( 'profile_comments', 'comment_id IN('.$final_ids.')' );
                                    break;
                    }
            }

            //-----------------------------------------
            // Bounce...
            //-----------------------------------------

            $this->ipsclass->boink_it( $this->ipsclass->base_url . 'act=profile&member_id='.$member_id.'&CODE=personal_iframe_comments&_saved=1&___msg=pp_comments_updated&md5check='.$this->ipsclass->md5_check );
    }

    /*-------------------------------------------------------------------------*/
    // Load friends tab
    /*-------------------------------------------------------------------------*/

    /**
    * Loads the content for the friends tab
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-15
    */
    function personal_iframe_friends()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id 		 = intval( $this->ipsclass->input['member_id'] );
            $md5check  		 = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $content   		 = '';
            $friends_perpage = 50;
            $pages           = '';
            $start			 = intval( $this->ipsclass->input['st'] );
            $friends         = array();

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            print '';
                    exit();
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    print '';
                    exit();
    }

            //-----------------------------------------
            // How many comments must a man write down
            // before he is considered a spammer?
            //-----------------------------------------

            $friend_count = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'count(*) as friend_count',
                                                                                                                                              'from'   => 'profile_friends',
                                                                                                                                              'where'  => 'friends_member_id='.$member_id . ' AND friends_approved=1' ) );

            //-----------------------------------------
            // Pages
            //-----------------------------------------

            $pages = $this->ipsclass->build_pagelinks( array(  'TOTAL_POSS'  => intval( $friend_count['friend_count'] ),
                                                                                                               'no_dropdown' => 1,
                                                                                                               'PER_PAGE'    => $friends_perpage,
                                                                                                               'CUR_ST_VAL'  => $start,
                                                                                                               'L_SINGLE'    => "",
                                                                                                               'BASE_URL'    => $this->ipsclass->base_url . 'act=profile&member_id='.$member_id.'&CODE=personal_iframe_friends&md5check='.$this->ipsclass->md5_check,
                                                                                                             ) );

            $pages = $this->ipsclass->compiled_templates['skin_profile']->personal_portal_page_link($pages);

            //-----------------------------------------
            // Grab the friends
            //-----------------------------------------

            $this->ipsclass->DB->build_query( array( 'select'   => 'f.*',
                                                                                             'from'     => array( 'profile_friends' => 'f' ),
                                                                                             'where'    => 'f.friends_member_id='.$member_id . ' AND f.friends_approved=1',
                                                                                             'limit'    => array( $start, $friends_perpage ),
                                                                                             'add_join' => array( 0 => array( 'select' => 'me.*',
                                                                                                                                                              'from'   => array( 'member_extra' => 'me' ),
                                                                                                                                                              'where'  => 'me.id=f.friends_friend_id',
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      1 => array( 'select' => 'pp.*',
                                                                                                                                                              'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                                                                              'where'  => 'pp.pp_member_id=f.friends_friend_id',
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      2 => array( 'select' => 'm.*',
                                                                                                                                                              'from'   => array( 'members' => 'm' ),
                                                                                                                                                              'where'  => 'm.id=f.friends_friend_id',
                                                                                                                                                              'type'   => 'left' ) ) ) );
            $this->ipsclass->DB->exec_query();

            //-----------------------------------------
            // Get and store...
            //-----------------------------------------

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                    //-----------------------------------------
                    // Others...
                    //-----------------------------------------

                    $row['_last_active']   = $this->ipsclass->get_date( $row['last_activity'], 'SHORT' );
                    // Added by BabyWolf
                    $row['name_url_key'] 			= strtolower($row['name']);

                    if( $row['login_anonymous']{0} == '1' )
                    {
                            // Member last logged in anonymous

                            if( $this->ipsclass->member['mgroup'] != $this->ipsclass->vars['admin_group'] OR $this->ipsclass->vars['disable_admin_anon'] )
                            {
                                    $row['_last_active'] = $this->ipsclass->lang['private'];
                            }
                    }

                    $row['_friends_added'] = $this->ipsclass->get_date( $row['friends_added'], 'SHORT' );
                    $row['g_title']        = $this->ipsclass->cache['group_cache'][ $row['mgroup'] ]['g_title'];

                    //-----------------------------------------
                    // Add row...
                    //-----------------------------------------

                    $friends[ $row['members_display_name'] ] = $this->personal_portal_set_information( $row );
            }

            //-----------------------------------------
            // Sort
            //-----------------------------------------

            ksort( $friends );

            //-----------------------------------------
            // Ok.. show the friends
            //-----------------------------------------

            $content = $this->ipsclass->compiled_templates['skin_profile']->personal_portal_iframe_friends( $member, $friends, $pages );

            $this->ipsclass->print->pop_up_window( '', $content );

    }

    /*-------------------------------------------------------------------------*/
    // Load comments tab
    /*-------------------------------------------------------------------------*/

    /**
    * Loads the content for the comments tab
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_iframe_comments()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id 		 = intval( $this->ipsclass->input['member_id'] );
            $md5check  		 = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $content   		 = '';
            $comment_perpage = $this->ipsclass->vars['social_per_page_setting'];
            $pages           = '';
            $start			 = intval( $this->ipsclass->input['st'] );
            $sql_extra       = '';

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            print '';
                    exit();
    }

            //-----------------------------------------
            // Not my tab? So no moderation...
            //-----------------------------------------

            if (  ( $member_id != $this->ipsclass->member['id'] ) AND ( ! $this->ipsclass->member['g_is_supmod'] ) )
    {
            $sql_extra = ' AND comment_approved=1';
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    print '';
                    exit();
    }

            //-----------------------------------------
            // How many comments must a man write down
            // before he is considered a spammer?
            //-----------------------------------------

            $comment_count = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'count(*) as count_comment',
                                                                                                                                               'from'   => 'profile_comments',
                                                                                                                                               'where'  => 'comment_for_member_id='.$member_id . $sql_extra ) );

            //-----------------------------------------
            // Pages
            //-----------------------------------------

            $pages = $this->ipsclass->build_pagelinks( array(  'TOTAL_POSS'  => intval( $comment_count['count_comment'] ),
                                                                                                               'PER_PAGE'    => $comment_perpage,
                                                                                                               'CUR_ST_VAL'  => $start,
                                                                                                               'L_SINGLE'    => "",
                                                                                                               'BASE_URL'    => $this->ipsclass->base_url . 'act=profile&member_id='.$member_id.'&CODE=personal_iframe_comments&md5check='.$this->ipsclass->md5_check,
                                                                                                             ) );

            $pages = $this->ipsclass->compiled_templates['skin_profile']->personal_portal_page_link($pages);

            //-----------------------------------------
            // Regenerate comments...
            //-----------------------------------------

            $this->ipsclass->DB->build_query( array( 'select'   => 'pc.*',
                                                                                             'from'     => array( 'profile_comments' => 'pc' ),
                                                                                             'where'    => 'pc.comment_for_member_id='.$member_id . $sql_extra,
                                                                                             'order'    => 'pc.comment_date DESC',
                                                                                             'limit'    => array( $start, $comment_perpage ),
                                                                                             'add_join' => array( 0 => array( 'select' => 'm.name, m.members_display_name, m.login_anonymous',
                                                                                                                                                              'from'   => array( 'members' => 'm' ),
                                                                                                                                                              'where'  => 'm.id=pc.comment_by_member_id',
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      1 => array( 'select' => 'pp.*',
                                                                                                                                                              'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                                                                              'where'  => 'pp.pp_member_id=m.id',
                                                                                                                                                              'type'   => 'left' ),	
                                                                                                                                      2 => array( 'select' => 'me.*',
                                                                                                                                                              'from'   => array( 'member_extra' => 'me' ),
                                                                                                                                                              'where'  => 'me.id=pc.comment_by_member_id',
                                                                                                                                                              'type'   => 'left' ) ) ) );

            $this->ipsclass->DB->exec_query();

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                    $row['_comment_date']   = $this->ipsclass->get_date( $row['comment_date'], 'SHORT' );
                    $row['_avatar']         = $this->ipsclass->get_avatar( $row['avatar_location'], $this->ipsclass->member['view_avs'], $row['avatar_size'], $row['avatar_type'] );
                    $row['_last_active']    = $this->ipsclass->get_date( $row['last_activity'], 'SHORT' );
                    // Added by BabyWolf
                    $row['name'] 			= strtolower($row['name']);

                    if( $row['login_anonymous']{0} == '1' )
                    {
                            // Member last logged in anonymous

                            if( $this->ipsclass->member['mgroup'] != $this->ipsclass->vars['admin_group'] OR $this->ipsclass->vars['disable_admin_anon'] )
                            {
                                    $row['_last_active'] = $this->ipsclass->lang['private'];
                            }
                    }

                    $row['comment_content'] = $this->ipsclass->txt_wordwrap( $row['comment_content'], '19', ' ' );

                    $row = $this->personal_portal_set_information( $row, 0, 0 );

                    $comments[] = $row;
            }

            //-----------------------------------------
            // Ok.. show the settings
            //-----------------------------------------

            // Added to here by BabyWolf
            if ( $member['pp_setting_count_friends'] > 0 )
            {
                    $member['_cache'] = $this->ipsclass->unpack_member_cache( $member['members_cache'] );
            }

            //-----------------------------------------
            // Custom fields
            //-----------------------------------------

            require_once( ROOT_PATH.'sources/classes/class_custom_fields.php' );
            $custom_fields = new custom_fields( $this->ipsclass->DB );

            $custom_fields->member_id  = $this->ipsclass->member['id'];
            $custom_fields->cache_data = $this->ipsclass->cache['profilefields'];
            $custom_fields->admin      = intval($this->ipsclass->member['g_access_cp']);
            $custom_fields->supmod     = intval($this->ipsclass->member['g_is_supmod']);

            $custom_fields->member_data = $member;
            $custom_fields->init_data();
            $custom_fields->parse_to_view( 0 );

            if ( count( $custom_fields->out_fields ) )
            {
                    foreach( $custom_fields->out_fields as $id => $data )
            {
                    //if ( ! $data )
                    //{
                            //$data = $this->ipsclass->lang['no_info'];
                    //}

            //	$data = $this->ipsclass->txt_wordwrap( $data, '25', ' ' );

                            //$member['custom_fields'][] = array( 'name' => $custom_fields->field_names[ $id ], 'data' => $data );
                            $member['custom_fields'] .= "\n".$custom_fields->method_format_field_for_topic_view( $id );
            }

                    // Added by BabyWolf
                    $member['dkm_Level'] = $custom_fields->field_data['Level'];
                    $member['dkm_Icon'] = $custom_fields->field_data['dkm_Icon'];
                    $member['dkm_Reputation'] = $custom_fields->field_data['dkm_Reputation'];
                    $member['dkm_Cash'] = $custom_fields->field_data['dkm_Cash'];
                    $member['dkm_HP'] = $custom_fields->field_data['HP'];
                    $member['dkm_MP'] = $custom_fields->field_data['MP'];
                    $member['dkm_EXP'] = $custom_fields->field_data['EXP'];	
                    $member['dkm_Topics'] = $custom_fields->field_data['dkm_Topics'];
            }

            //-----------------------------------------
            // Format extra user data
            //-----------------------------------------

            $member['_age']             = ( $member['bday_year'] ) ? date( 'Y' ) - $member['bday_year'] : 0;

            if( $member['bday_month'] > date( 'n' ) )
            {
                    $member['_age'] -= 1;
            }
            else if( $member['bday_month'] == date( 'n' ) )
            {
                    if( $member['bday_day'] > date( 'j' ) )
                    {
                            $member['_age'] -= 1;
                    }
            }

            $member['_bday_month'] = $this->get_month_name( $member['bday_month'] );

            // Set avatar size by BabyWolf
            $av_size = $this->ipsclass->vars['dkm_av_size'];
            if($member['field_'.$this->ipsclass->vars['dkm_avatar_field_id']]) {
                    $av_size = $this->ipsclass->vars['dkm_av_bought_size'];	
            }

    $member['_avatar']           = $this->ipsclass->get_avatar( $member['avatar_location'], $this->ipsclass->member['view_avs'], $av_size, $member['avatar_type'] );
            $member['_email']            = $member['hide_email'] ? $this->ipsclass->lang['private'] : "<a href='{$this->ipsclass->base_url}act=Mail&amp;CODE=00&amp;MID={$member['id']}'>{$this->ipsclass->lang['email']}</a>";
            $member['_website'] 		 = ( preg_match( "/^http:\/\/\S+$/", $member['website'] ) ) ? $member['website'] : '';

            //-----------------------------------------
            // Contact info
            //-----------------------------------------

            $member['icq_number']  = $member['icq_number'] > 0 ? $member['icq_number'] : '';		

            $member['_aim_name']   = $member['aim_name']   ? $this->ipsclass->txt_wordwrap( $member['aim_name'], '25' )   : $this->ipsclass->lang['no_info'];
    $member['_icq_number'] = $member['icq_number'] ? $this->ipsclass->txt_wordwrap( $member['icq_number'], '25' ) : $this->ipsclass->lang['no_info'];
    $member['_yahoo']      = $member['yahoo']      ? $this->ipsclass->txt_wordwrap( $member['yahoo'], '25' )      : $this->ipsclass->lang['no_info'];
    $member['_msn_name']   = $member['msnname']    ? $this->ipsclass->txt_wordwrap( $member['msnname'], '25' )    : $this->ipsclass->lang['no_info'];

            $content = $this->ipsclass->compiled_templates['skin_profile']->personal_portal_iframe_comments( $member, $comments, $pages );

            $this->ipsclass->print->pop_up_window( '', $content );
    }

    /*-------------------------------------------------------------------------*/
    // Save settings tab
    /*-------------------------------------------------------------------------*/

    /**
    * Saves the content for the settings tab
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_iframe_settings_save()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id 					  = intval( $this->ipsclass->input['member_id'] );
            $md5check  					  = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $pp_setting_notify_comments   = trim( substr( $this->ipsclass->input['pp_setting_notify_comments'], 0, 10 ) );
            $pp_setting_notify_friend     = trim( substr( $this->ipsclass->input['pp_setting_notify_friend'], 0, 10 ) );
            $pp_setting_moderate_comments = intval( $this->ipsclass->input['pp_setting_moderate_comments'] );
            $pp_setting_moderate_friends  = intval( $this->ipsclass->input['pp_setting_moderate_friends'] );
            $pp_bio_content				  = $this->ipsclass->txt_mbsubstr( $this->ipsclass->my_nl2br( $this->ipsclass->input['pp_bio_content'] ), 0, 300 );
            $website					  = trim( $this->ipsclass->input['website'] );

            //-----------------------------------------
            // Settings...
            //-----------------------------------------

            foreach( array( 'pp_setting_count_friends', 'pp_setting_count_comments', 'pp_setting_count_visitors' ) as $item )
            {
                    $_val = 0;

                    switch( $this->ipsclass->input[ $item ] )
                    {
                            default:
                            case '3':
                                    $_val = 3;
                                    break;
                            case '5':
                                    $_val = 5;
                                    break;
                            case '10':
                                    $_val = 10;
                                    break;
                            case '0':
                                    $_val = -1;
                                    break;
                    }

                    ${$item} = $_val;
            }

            //-----------------------------------------
            // Clean website...
            //-----------------------------------------

            $website = ( preg_match( "#^http://[a-z0-9\.\-].*$#i", $website ) ) ? $website : '';

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            $this->ipsclass->boink_it( $this->ipsclass->base_url . 'act=profile&amp;member_id='.$member_id.'&amp;CODE=personal_iframe_settings&amp;___msg=no_permission&md5check='.$this->ipsclass->md5_check );
                    exit();
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // My tab?
            //-----------------------------------------

            if (  ( $member_id != $this->ipsclass->member['id'] ) AND ( ! $this->ipsclass->member['g_is_supmod'] OR ( $member['mgroup'] == $this->ipsclass->vars['admin_group'] ) ) )
    {
            $this->ipsclass->boink_it( $this->ipsclass->base_url . 'act=profile&amp;member_id='.$member_id.'&amp;CODE=personal_iframe_settings&amp;___msg=no_permission&md5check='.$this->ipsclass->md5_check );
                    exit();
    }		

            if( !$this->ipsclass->member['g_edit_profile'] )
            {
                    $website 		= ''; // Setting to nothing will prevent the query
                    $pp_bio_content	= $member['pp_bio_content'];
            }

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    $this->ipsclass->boink_it( $this->ipsclass->base_url . 'act=profile&amp;member_id='.$member_id.'&amp;CODE=personal_iframe_settings&amp;___msg=no_permission&md5check='.$this->ipsclass->md5_check );
                    exit();
    }

    if ( $this->ipsclass->member['g_edit_profile'] )
    {
                    //-----------------------------------------
                    // "Do" photo
                    //-----------------------------------------

                    require_once( ROOT_PATH . 'sources/lib/func_usercp.php' );
                    $func_70s_style           =  new func_usercp;
                    $func_70s_style->ipsclass =& $this->ipsclass;

                    $photo = $func_70s_style->lib_upload_photo();

                    if ( $photo['status'] == 'fail' )
                    {
                            //-----------------------------------------
                            // Save...
                            //-----------------------------------------

                            $this->ipsclass->DB->simple_construct( array( 'select' => 'pp_member_id',
                                                                                                                      'from'   => 'profile_portal',
                                                                                                                      'where'  => "pp_member_id=".$member_id ) );
                            $this->ipsclass->DB->simple_exec();

                            if ( $this->ipsclass->DB->get_num_rows() )
                            {
                                    # Update...
                                    $this->ipsclass->DB->do_update( 'profile_portal', array( 'pp_main_photo'                => $photo['final_location'],
                                                                                                                                                     'pp_main_width'                => intval( $photo['final_width'] ),
                                                                                                                                                     'pp_main_height'               => intval( $photo['final_height'] ),
                                                                                                                                                     'pp_thumb_photo'               => $photo['t_final_location'],
                                                                                                                                                     'pp_thumb_width'               => intval( $photo['t_final_width'] ),
                                                                                                                                                     'pp_thumb_height'              => intval( $photo['t_final_height'] ),
                                                                                                                                             ), 'pp_member_id='.$member_id );
                            }
                            else
                            {
                                    # Insert
                                    $this->ipsclass->DB->do_insert( 'profile_portal', array( 'pp_main_photo'                => $photo['final_location'],
                                                                                                                                                     'pp_main_width'                => intval( $photo['final_width'] ),
                                                                                                                                                     'pp_main_height'               => intval( $photo['final_height'] ),
                                                                                                                                                     'pp_thumb_photo'               => $photo['t_final_location'],
                                                                                                                                                     'pp_thumb_width'               => intval( $photo['t_final_width'] ),
                                                                                                                                                     'pp_thumb_height'              => intval( $photo['t_final_height'] ),
                                                                                                                                                     'pp_member_id'                 => $member_id,
                                                                                                                                             ) );
                            }

                            $this->personal_iframe_settings( $this->ipsclass->lang[ 'pp_' . $photo['error'] ] );
                    }
            }

            # Preserve old settings?
            if ( ! $photo['final_location'] AND $photo['status'] != 'deleted' )
            {
                    $photo['final_location']   = $member['pp_main_photo'];
                    $photo['final_width']      = $member['pp_main_width'];
                    $photo['final_height']     = $member['pp_main_height'];

                    $photo['t_final_location'] = $member['pp_thumb_photo'];
                    $photo['t_final_width']    = $member['pp_thumb_width'];
                    $photo['t_final_height']   = $member['pp_thumb_height'];
            }

            //-----------------------------------------
            // Save...
            //-----------------------------------------

            $this->ipsclass->DB->simple_construct( array( 'select' => 'pp_member_id',
                                                                                                      'from'   => 'profile_portal',
                                                                                                      'where'  => "pp_member_id=".$member_id ) );
            $this->ipsclass->DB->simple_exec();

            if ( $this->ipsclass->DB->get_num_rows() )
            {
                    # Update...
                    $this->ipsclass->DB->do_update( 'profile_portal', array( 'pp_main_photo'                => $photo['final_location'],
                                                                                                                                     'pp_main_width'                => intval( $photo['final_width'] ),
                                                                                                                                     'pp_main_height'               => intval( $photo['final_height'] ),
                                                                                                                                     'pp_thumb_photo'               => $photo['t_final_location'],
                                                                                                                                     'pp_thumb_width'               => intval( $photo['t_final_width'] ),
                                                                                                                                     'pp_thumb_height'              => intval( $photo['t_final_height'] ),
                                                                                                                                     'pp_bio_content'				=> $pp_bio_content,
                                                                                                                                     'pp_setting_notify_comments'   => $pp_setting_notify_comments,
                                                                                                                                     'pp_setting_notify_friend'     => $pp_setting_notify_friend,
                                                                                                                                     'pp_setting_moderate_comments' => $pp_setting_moderate_comments,
                                                                                                                                     'pp_setting_moderate_friends'  => $pp_setting_moderate_friends,
                                                                                                                                     'pp_setting_count_friends'     => $pp_setting_count_friends,
                                                                                                                                     'pp_setting_count_comments'    => $pp_setting_count_comments,
                                                                                                                                     'pp_setting_count_visitors'    => $pp_setting_count_visitors
                                                                                                                             ), 'pp_member_id='.$member_id );
            }
            else
            {
                    # Insert
                    $this->ipsclass->DB->do_insert( 'profile_portal', array( 'pp_main_photo'                => $photo['final_location'],
                                                                                                                                     'pp_main_width'                => intval( $photo['final_width'] ),
                                                                                                                                     'pp_main_height'               => intval( $photo['final_height'] ),
                                                                                                                                     'pp_thumb_photo'               => $photo['t_final_location'],
                                                                                                                                     'pp_thumb_width'               => intval( $photo['t_final_width'] ),
                                                                                                                                     'pp_thumb_height'              => intval( $photo['t_final_height'] ),
                                                                                                                                     'pp_bio_content'				=> $pp_bio_content,
                                                                                                                                     'pp_member_id'                 => $member_id,
                                                                                                                                     'pp_setting_notify_comments'   => $pp_setting_notify_comments,
                                                                                                                                     'pp_setting_notify_friend'     => $pp_setting_notify_friend,
                                                                                                                                     'pp_setting_moderate_comments' => $pp_setting_moderate_comments,
                                                                                                                                     'pp_setting_moderate_friends'  => $pp_setting_moderate_friends,
                                                                                                                                     'pp_setting_count_friends'     => $pp_setting_count_friends,
                                                                                                                                     'pp_setting_count_comments'    => $pp_setting_count_comments,
                                                                                                                                     'pp_setting_count_visitors'    => $pp_setting_count_visitors
                                                                                                                             ) );
            }

            //-----------------------------------------
            // Do website...
            //-----------------------------------------

            if ( $this->ipsclass->member['g_edit_profile'] AND $website != 'http://' )
            {
                    $this->ipsclass->DB->do_update( 'member_extra', array( 'website' => $website ), 'id='. $member_id );
            }

            //-----------------------------------------
            // Done...
            //-----------------------------------------

            $this->ipsclass->boink_it( $this->ipsclass->base_url . 'act=profile&member_id='.$member_id.'&CODE=personal_iframe_settings&_saved=1&___msg=settings_updated&md5check='.$this->ipsclass->md5_check );
    }

    /*-------------------------------------------------------------------------*/
    // Load settings tab
    /*-------------------------------------------------------------------------*/

    /**
    * Loads the content for the settings tab
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_iframe_settings( $error='' )
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id = intval( $this->ipsclass->input['member_id'] );
            $md5check  = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $content   = '';
            $types     = array( 'none'  => $this->ipsclass->lang['op_dd_none'],
                                                    'email' => $this->ipsclass->lang['op_dd_email'],
                                                    'pm'    => $this->ipsclass->lang['op_dd_pm'] );

            $yes_no    = array( '0'     => $this->ipsclass->lang['op_dd_disabled'],
                                                    '1'     => $this->ipsclass->lang['op_dd_enabled'] );

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            print '';
                    exit();
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // My tab?
            //-----------------------------------------

            if (  ( $member_id != $this->ipsclass->member['id'] ) AND ( ! $this->ipsclass->member['g_is_supmod'] OR ( $member['mgroup'] == $this->ipsclass->vars['admin_group'] ) ) )
    {
            print '';
                    exit();
    }		

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    print '';
                    exit();
    }

            //-----------------------------------------
            // Format settings...
            //-----------------------------------------

            foreach( $types as $key => $lang )
            {
                    $_comments_selected = ( $key == $member['pp_setting_notify_comments'] ) ? ' selected="selected" ' : '';
                    $_friends_selected  = ( $key == $member['pp_setting_notify_friend'] ) ? ' selected="selected" ' : '';
                    $member['_pp_setting_notify_comments'] .= "<option value='".$key."'".$_comments_selected.">".$lang."</option>";
                    $member['_pp_setting_notify_friend']   .= "<option value='".$key."'".$_friends_selected.">".$lang."</option>";
            }

            foreach( $yes_no as $key => $lang )
            {
                    $_comments_selected = ( $key == $member['pp_setting_moderate_comments'] ) ? ' selected="selected" ' : '';
                    $_friends_selected  = ( $key == $member['pp_setting_moderate_friends'] )  ? ' selected="selected" ' : '';
                    $member['_pp_setting_moderate_comments'] .= "<option value='".$key."'".$_comments_selected.">".$lang."</option>";
                    $member['_pp_setting_moderate_friends']  .= "<option value='".$key."'".$_friends_selected.">".$lang."</option>";
            }

            //-----------------------------------------
            // Get friends pending approval
            //-----------------------------------------

            $friends = $this->ipsclass->DB->build_and_exec_query( array( 'select' => 'COUNT(*) as count_friends',
                                                                                                                                     'from'   => 'profile_friends',
                                                                                                                                     'where'  => 'friends_friend_id='.$member['id'].' AND friends_approved=0' ) );

            //-----------------------------------------
            // Photo settings...
            //-----------------------------------------

            list( $size, $width, $height ) = explode( ':', $this->ipsclass->member['g_photo_max_vars'] );
            $photo_ext                     = str_replace( ',', '/', $this->ipsclass->vars['photo_ext'] );

            $this->ipsclass->lang['pp_photo_desc'] = sprintf( $this->ipsclass->lang['pp_photo_desc'], $width, $height, $size, $photo_ext );

            # Allow extra for compression
            $member['_max_file_size'] = ( $size * 1024 ) * 4;

            //-----------------------------------------
            // Personal statement
            //-----------------------------------------

            $member['_pp_bio_content']  = $this->ipsclass->my_br2nl( $member['pp_bio_content'] );
            $member['__pp_bio_content'] = $member['pp_bio_content'];

            //-----------------------------------------
            // Ok.. show the settings
            //-----------------------------------------

            $content = $this->ipsclass->compiled_templates['skin_profile']->personal_portal_iframe_settings( $member, $friends['count_friends'], $error );

            $this->ipsclass->print->pop_up_window( '', $content );
    }

    /*-------------------------------------------------------------------------*/
    // Builds comments
    /*-------------------------------------------------------------------------*/

    /**
    * Builds comments
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_build_comments( $member, $new_id=0, $return_msg='' )
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $comments        = array();
            $member_id       = intval( $member['id'] );
            $comment_perpage = intval( $member['pp_setting_count_comments'] );
            $comment_html    = 0;

            //-----------------------------------------
            // Choosing to show comments?
            //-----------------------------------------

            if ( $comment_perpage < 1 )
            {
                    return '';
            }

            //-----------------------------------------
            // Regenerate comments...
            //-----------------------------------------

            $this->ipsclass->DB->build_query( array( 'select'   => 'pc.*',
                                                                                             'from'     => array( 'profile_comments' => 'pc' ),
                                                                                             'where'    => 'pc.comment_for_member_id='.$member_id.' AND pc.comment_approved=1',
                                                                                             'order'    => 'pc.comment_date DESC',
                                                                                             'limit'    => array( 0, $comment_perpage ),
                                                                                             'add_join' => array( 0 => array( 'select' => 'm.members_display_name, m.posts, m.last_activity,m.name',
                                                                                                                                                              'from'   => array( 'members' => 'm' ),
                                                                                                                                                              'where'  => 'm.id=pc.comment_by_member_id',
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      1 => array( 'select' => 'pp.*',
                                                                                                                                                              'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                                                                              'where'  => 'pp.pp_member_id=m.id',
                                                                                                                                                              'type'   => 'left' ),	
                                                                                                                                      2 => array( 'select' => 'me.*',
                                                                                                                                                              'from'   => array( 'member_extra' => 'me' ),
                                                                                                                                                              'where'  => 'me.id=pc.comment_by_member_id',
                                                                                                                                                              'type'   => 'left' ) ) ) );

            $this->ipsclass->DB->exec_query();

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                    $row['_comment_date']   = $this->ipsclass->get_date( $row['comment_date'], 'TINY' );
                    $row['_avatar']         = $this->ipsclass->get_avatar( $row['avatar_location'], $this->ipsclass->member['view_avs'], $row['avatar_size'], $row['avatar_type'] );
                    $row['_last_active']    = $this->ipsclass->get_date( $row['last_activity'], 'TINY' );

                    if( $row['login_anonymous']{0} == '1' )
                    {
                            // Member last logged in anonymous

                            if( $this->ipsclass->member['mgroup'] != $this->ipsclass->vars['admin_group'] OR $this->ipsclass->vars['disable_admin_anon'] )
                            {
                                    $row['_last_active'] = $this->ipsclass->lang['private'];
                            }
                    }

                    $row['comment_content'] = $this->ipsclass->txt_wordwrap( $row['comment_content'], '19', ' ' );

                    $row = $this->personal_portal_set_information( $row, 0, 0 );
                    $row['pp_mini_width']  = 40;
                    $row['pp_mini_height']  = 40;
                    $comments[] = $row;
            }		
            $comment_html = $this->ipsclass->compiled_templates['skin_profile']->personal_portal_show_comment( $comments, $member, $new_id, $return_msg );

            //-----------------------------------------
            // Return it...
            //-----------------------------------------

            return $comment_html;
    }

    /*-------------------------------------------------------------------------*/
    // Reload comments
    /*-------------------------------------------------------------------------*/

    /**
    * Reload comments
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-15
    */
    function personal_ajax_reload_comments()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id       = intval( $this->ipsclass->input['member_id'] );
            $md5check        = substr( $this->ipsclass->input['md5check'], 0, 32 );

            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
    }

            //-----------------------------------------
            // Regenerate comments...
            //-----------------------------------------

            $class_ajax->return_html( $this->personal_build_comments( $member ) );
            exit();
    }

    /*-------------------------------------------------------------------------*/
    // Delete comment
    /*-------------------------------------------------------------------------*/

    /**
    * Deletes a comment on member's profile
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_ajax_delete_comment()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id       = intval( $this->ipsclass->input['member_id'] );
            $md5check        = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $comment_id      = intval( $this->ipsclass->input['comment_id'] );
            $comment_html    = "";
            $comment_perpage = 5;

            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
    }

            //-----------------------------------------
            // Can remove?
            //-----------------------------------------

            if ( ( $member['id'] == $this->ipsclass->member['id'] ) OR $this->ipsclass->member['g_is_supmod'] )
            {
                    $this->ipsclass->DB->build_and_exec_query( array( 'delete' => 'profile_comments',
                                                                                                                      'where'  => 'comment_id='.$comment_id ) );
            }

            //-----------------------------------------
            // Regenerate comments...
            //-----------------------------------------

            $class_ajax->return_html( $this->personal_build_comments( $member ) );
            exit();
    }

    /*-------------------------------------------------------------------------*/
    // Load dialog between two members by BabyWolf
    /*-------------------------------------------------------------------------*/

    /**
    * Builds comments
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_load_dialog($owner, $member)
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $comments        = array();
            $member_id       = intval( $member['id'] );
            $owner_id		 = intval( $owner['id'] );
            $comment_perpage = intval( $owner['pp_setting_count_comments'] );
            $comment_html    = 0;

            //-----------------------------------------
            // Choosing to show comments?
            //-----------------------------------------

            if ( $comment_perpage < 1 )
            {
                    return '';
            }

            //-----------------------------------------
            // Regenerate comments...
            //-----------------------------------------

            $this->ipsclass->DB->build_query( array( 'select'   => 'pc.*',
                                                                                             'from'     => array( 'profile_comments' => 'pc' ),
                                                                                             'where'    => '((pc.comment_for_member_id='.$owner_id.' AND pc.comment_by_member_id='.$member_id.') OR 
                                                                                                                            (pc.comment_for_member_id='.$member_id.' AND pc.comment_by_member_id='.$owner_id.')) AND pc.comment_approved=1',
                                                                                             'order'    => 'pc.comment_date DESC',
                                                                                             'limit'    => array( 0, $comment_perpage ),
                                                                                             'add_join' => array( 0 => array( 'select' => 'm.members_display_name, m.posts, m.last_activity,m.name',
                                                                                                                                                              'from'   => array( 'members' => 'm' ),
                                                                                                                                                              'where'  => 'm.id=pc.comment_by_member_id',
                                                                                                                                                              'type'   => 'left' ),
                                                                                                                                      1 => array( 'select' => 'pp.*',
                                                                                                                                                              'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                                                                              'where'  => 'pp.pp_member_id=m.id',
                                                                                                                                                              'type'   => 'left' ),	
                                                                                                                                      2 => array( 'select' => 'me.*',
                                                                                                                                                              'from'   => array( 'member_extra' => 'me' ),
                                                                                                                                                              'where'  => 'me.id=pc.comment_by_member_id',
                                                                                                                                                              'type'   => 'left' ) ) ) );

            $this->ipsclass->DB->exec_query();

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                    $row['_comment_date']   = $this->ipsclass->get_date( $row['comment_date'], 'TINY' );
                    $row['_avatar']         = $this->ipsclass->get_avatar( $row['avatar_location'], $this->ipsclass->member['view_avs'], $row['avatar_size'], $row['avatar_type'] );
                    $row['_last_active']    = $this->ipsclass->get_date( $row['last_activity'], 'TINY' );

                    if( $row['login_anonymous']{0} == '1' )
                    {
                            // Member last logged in anonymous

                            if( $this->ipsclass->member['mgroup'] != $this->ipsclass->vars['admin_group'] OR $this->ipsclass->vars['disable_admin_anon'] )
                            {
                                    $row['_last_active'] = $this->ipsclass->lang['private'];
                            }
                    }

                    $row['comment_content'] = $this->ipsclass->txt_wordwrap( $row['comment_content'], '19', ' ' );

                    $row = $this->personal_portal_set_information( $row, 0, 0 );
                    $row['pp_mini_width']  = 40;
                    $row['pp_mini_height']  = 40;
                    $comments[] = $row;
            }		
            $comment_html = $this->ipsclass->compiled_templates['skin_profile']->personal_portal_show_comment( $comments, $member, $new_id, $return_msg );

            //-----------------------------------------
            // Return it...
            //-----------------------------------------

            return $comment_html;
    }

    /*-------------------------------------------------------------------------*/
    // Reply comment by BabyWolf
    /*-------------------------------------------------------------------------*/

    /**
    * Reply a comment on member's profile
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_ajax_load_dialog()
    {
            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id        = intval( $this->ipsclass->input['member_id'] );
            $md5check         = substr( $this->ipsclass->input['md5check'], 0, 32 );

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'nopermission';
                    exit();
    }

            //-----------------------------------------
            // Regenerate comments...
            //-----------------------------------------
            $this_member = $this->personal_function_load_member($this->ipsclass->member['id']);
            $dialog_html = $this->personal_load_dialog($this_member, $member );
            $return_html = $this->ipsclass->compiled_templates['skin_profile']->personal_display_dialog($dialog_html, $member);

            $class_ajax->return_html( $return_html );
            exit();
    }

    /*-------------------------------------------------------------------------*/
    // Reply comment by BabyWolf
    /*-------------------------------------------------------------------------*/

    /**
    * Reply a comment on member's profile
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_ajax_reply_comment()
    {
            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id        = intval( $this->ipsclass->input['member_id'] );
            $md5check         = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $comment          = '';
            $comment_approved = 1;
            $message		  = '';
            $subject		  = '';
            $to               = array();
            $from             = array();
            $return_msg       = '';

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'nopermission';
                    exit();
    }

            //-----------------------------------------
            // Member allowed to post at all?
            //-----------------------------------------

    if ( ! $this->ipsclass->member['g_reply_other_topics'] )
    {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'nopermission';
                    exit();
    }

            if ( $this->ipsclass->member['restrict_post'] )
    {
                    if ( $this->ipsclass->member['restrict_post'] == 1 )
            {
                            @header( "Content-type: text/plain" );
                            $class_ajax->print_nocache_headers();
                            print 'nopermission';
                            exit();
                    }

                    $post_arr = $this->ipsclass->hdl_ban_line( $this->ipsclass->member['restrict_post'] );

                    if ( time() >= $post_arr['date_end'] )
                    {
                            //-----------------------------------------
                    // Update this member's profile
                            //-----------------------------------------

                            $this->ipsclass->DB->simple_construct( array( 'update' => 'members',
                                                                                                                      'set'    => 'restrict_post=0',
                                                                                                                      'where'  => "id=".intval($this->ipsclass->member['id'])
                                                                                                    )       );
                            $this->ipsclass->DB->simple_exec();
            }
            else
            {
                            @header( "Content-type: text/plain" );
                            $class_ajax->print_nocache_headers();
                            print 'nopermission';
                            exit();
                    }
            }         

            //-----------------------------------------
            // Does this member have mod_posts enabled?
            //-----------------------------------------

            if ( isset($this->ipsclass->member['mod_posts']) AND $this->ipsclass->member['mod_posts'] )
            {
                    if ( $this->ipsclass->member['mod_posts'] == 1 )
                    {
                            $comment_approved = 0;
                    }
                    else
                    {
                            $mod_arr = $this->ipsclass->hdl_ban_line( $this->ipsclass->member['mod_posts'] );

                            if ( time() >= $mod_arr['date_end'] )
                            {
                                    //-----------------------------------------
                                    // Update this member's profile
                                    //-----------------------------------------

                                    $this->ipsclass->DB->simple_construct( array( 'update' => 'members',
                                                                                                                              'set'    => 'mod_posts=0',
                                                                                                                              'where'  => "id=".intval($this->ipsclass->member['id'])
                                                                                                         )       );
                                    $this->ipsclass->DB->simple_exec();
                            }
                            else
                            {
                                    $comment_approved = 0;
                            }
                    }
            }

            //-----------------------------------------
    // Get the emailer module
            //-----------------------------------------

            require_once( ROOT_PATH."sources/classes/class_email.php" );

            $email           =  new emailer();
            $email->ipsclass =& $this->ipsclass;
            $email->email_init();

            //-----------------------------------------
            // Get MSG library class
            //-----------------------------------------

            require_once( ROOT_PATH.'sources/lib/func_msg.php' );

            $msg_lib           =  new func_msg();
            $msg_lib->ipsclass =& $this->ipsclass;
            $msg_lib->init();
            $this->ipsclass->load_language( 'lang_msg' );

            //-----------------------------------------
            // Finish up
            //-----------------------------------------

            $_POST['comment'] = $class_ajax->convert_unicode( $_POST['comment'] );
            $_POST['comment'] = $class_ajax->convert_html_entities( $_POST['comment'] );

            $comment = $this->ipsclass->parse_clean_value( $_POST['comment'] );
            $comment = $class_ajax->ipsclass->txt_mbsubstr( $comment, 0, 400 );
            $comment = preg_replace( "#(\r\n|\r|\n|<br />|<br>){1,}#s", "\n", $comment );

            //-----------------------------------------
            // Bad words
            //-----------------------------------------

            $comment = trim( $this->parser->bad_words( $comment ) );

            //-----------------------------------------
            // Got a comment?
            //-----------------------------------------

            if ( ! $comment )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error-no-comment';
                    exit();
    }

            //-----------------------------------------
            // Friend requires approval?
            //-----------------------------------------

            if ( $member['pp_setting_moderate_comments'] AND $member['id'] != $this->ipsclass->member['id'] )
            {
                    $comment_approved = 0;
            }

            //-----------------------------------------
            // Member is ignoring you!
            //-----------------------------------------

            if ( $comment_approved )
            { 
                    $_you_are_being_ignored = explode( ",", $member['ignored_users'] );

                    if ( is_array( $_you_are_being_ignored ) and count( $_you_are_being_ignored ) )
                    {
                            if ( in_array( $this->ipsclass->member['id'], $_you_are_being_ignored ) )
                            {
                                    $comment_approved = 0;
                            }
                    }
            }

            //-----------------------------------------
            // Add comment to the DB...
            //-----------------------------------------

            $this->ipsclass->DB->do_insert( 'profile_comments', array( 'comment_for_member_id' => $member_id,
                                                                                                                               'comment_by_member_id'  => $this->ipsclass->member['id'],
                                                                                                                               'comment_date'		   => time(),
                                                                                                                               'comment_ip_address'    => $this->ipsclass->ip_address,
                                                                                                                               'comment_approved'      => $comment_approved,
                                                                                                                               'comment_content'	   => $this->ipsclass->my_nl2br( $comment ) ) );

            $new_id = $this->ipsclass->DB->get_insert_id();

            //-----------------------------------------
            // What are we doing? Sending out 'mod' or 'yay'
            // message?
            //-----------------------------------------

            if ( ! $comment_approved AND $member['pp_setting_notify_comments'] AND ( $member['id'] != $this->ipsclass->member['id'] ) )
            {
                    $email->get_template("new_comment_request");

                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $member['members_display_name'],
                                                                              'COMMENT_NAME'         => $this->ipsclass->member['members_display_name'],
                                                                              'LINK'				 => $this->ipsclass->vars['board_url'] . '/index.' . $this->ipsclass->vars['php_ext'] . '?act=profile&CODE=personal_portal_view&tab=comments&id='.$member_id ) );

                    $message    = $email->message;
                    $subject    = $email->lang_subject;
                    $to         = $member;
                    $from       = $this->ipsclass->member;
                    $return_msg = 'pp_comment_added_mod';
            }
            else if ( $member['pp_setting_notify_comments'] AND ( $member['id'] != $this->ipsclass->member['id'] ) )
            {
                    $email->get_template("new_comment_added");

                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $member['members_display_name'],
                                                                              'COMMENT_NAME'         => $this->ipsclass->member['members_display_name'],
                                                                              'LINK'				 => $this->ipsclass->vars['board_url'] . '/index.' . $this->ipsclass->vars['php_ext'] . '?act=profile&CODE=personal_portal_view&tab=comments&id='.$member_id ) );

                    $message    = $email->message;
                    $subject    = $email->lang_subject;
                    $to         = $member;
                    $from       = $this->ipsclass->member;
                    $return_msg = '';
            }

            //-----------------------------------------
            // Got anything to send?
            //-----------------------------------------

            if ( $message AND $subject )
            {
                    //-----------------------------------------
                    // Email?
                    //-----------------------------------------

                    if ( $member['pp_setting_notify_comments'] == 'email' OR ( $member['pp_setting_notify_comments'] AND $member['members_disable_pm'] ) )
                    {
                            $email->subject = $subject;
                            $email->message = $message;
                            $email->to      = $to['email'];

                            $email->send_mail();
                    }

                    //-----------------------------------------
                    // PM?
                    //-----------------------------------------

                    else if ( $member['pp_setting_notify_comments'] != 'none' )
                    {
                            $msg_lib->to_by_id    = $to['id'];
                            $msg_lib->from_member = $from;
                            $msg_lib->msg_title   = $subject;

                            $msg_lib->postlib->parser->parse_bbcode 	= 1;
                            $msg_lib->postlib->parser->parse_smilies 	= 0;
                            $msg_lib->postlib->parser->parse_html 		= 0;
                            $msg_lib->postlib->parser->parse_nl2br 		= 1;
                            $msg_lib->msg_post    = $msg_lib->postlib->parser->pre_display_parse( $msg_lib->postlib->parser->pre_db_parse( $message ) );

                            $msg_lib->force_pm    = 1;

                            $msg_lib->send_pm();
                    }
            }

            //-----------------------------------------
            // Regenerate comments...
            //-----------------------------------------
            $this_member = $this->personal_function_load_member($this->ipsclass->member['id']);
            $returnHTML = $this->personal_build_comments( $this_member, $new_id, $return_msg );
            $returnHTML = $this->ipsclass->lang['bw_personal_portal_reply_sent'].$member['members_display_name'].$returnHTML;

            $class_ajax->return_html( $returnHTML );
            exit();
    }

    /*-------------------------------------------------------------------------*/
    // Save comment
    /*-------------------------------------------------------------------------*/

    /**
    * Saves a comment on member's profile
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_ajax_add_comment()
    {
            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id        = intval( $this->ipsclass->input['member_id'] );
            $md5check         = substr( $this->ipsclass->input['md5check'], 0, 32 );
            $comment          = '';
            $comment_approved = 1;
            $message		  = '';
            $subject		  = '';
            $to               = array();
            $from             = array();
            $return_msg       = '';

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'nopermission';
                    exit();
    }

            //-----------------------------------------
            // Member allowed to post at all?
            //-----------------------------------------

    if ( ! $this->ipsclass->member['g_reply_other_topics'] )
    {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'nopermission';
                    exit();
    }

            if ( $this->ipsclass->member['restrict_post'] )
    {
                    if ( $this->ipsclass->member['restrict_post'] == 1 )
            {
                            @header( "Content-type: text/plain" );
                            $class_ajax->print_nocache_headers();
                            print 'nopermission';
                            exit();
                    }

                    $post_arr = $this->ipsclass->hdl_ban_line( $this->ipsclass->member['restrict_post'] );

                    if ( time() >= $post_arr['date_end'] )
                    {
                            //-----------------------------------------
                    // Update this member's profile
                            //-----------------------------------------

                            $this->ipsclass->DB->simple_construct( array( 'update' => 'members',
                                                                                                                      'set'    => 'restrict_post=0',
                                                                                                                      'where'  => "id=".intval($this->ipsclass->member['id'])
                                                                                                    )       );
                            $this->ipsclass->DB->simple_exec();
            }
            else
            {
                            @header( "Content-type: text/plain" );
                            $class_ajax->print_nocache_headers();
                            print 'nopermission';
                            exit();
                    }
            }         

            //-----------------------------------------
            // Does this member have mod_posts enabled?
            //-----------------------------------------

            if ( isset($this->ipsclass->member['mod_posts']) AND $this->ipsclass->member['mod_posts'] )
            {
                    if ( $this->ipsclass->member['mod_posts'] == 1 )
                    {
                            $comment_approved = 0;
                    }
                    else
                    {
                            $mod_arr = $this->ipsclass->hdl_ban_line( $this->ipsclass->member['mod_posts'] );

                            if ( time() >= $mod_arr['date_end'] )
                            {
                                    //-----------------------------------------
                                    // Update this member's profile
                                    //-----------------------------------------

                                    $this->ipsclass->DB->simple_construct( array( 'update' => 'members',
                                                                                                                              'set'    => 'mod_posts=0',
                                                                                                                              'where'  => "id=".intval($this->ipsclass->member['id'])
                                                                                                         )       );
                                    $this->ipsclass->DB->simple_exec();
                            }
                            else
                            {
                                    $comment_approved = 0;
                            }
                    }
            }

            //-----------------------------------------
    // Get the emailer module
            //-----------------------------------------

            require_once( ROOT_PATH."sources/classes/class_email.php" );

            $email           =  new emailer();
            $email->ipsclass =& $this->ipsclass;
            $email->email_init();

            //-----------------------------------------
            // Get MSG library class
            //-----------------------------------------

            require_once( ROOT_PATH.'sources/lib/func_msg.php' );

            $msg_lib           =  new func_msg();
            $msg_lib->ipsclass =& $this->ipsclass;
            $msg_lib->init();
            $this->ipsclass->load_language( 'lang_msg' );

            //-----------------------------------------
            // Finish up
            //-----------------------------------------

            $_POST['comment'] = $class_ajax->convert_unicode( $_POST['comment'] );
            $_POST['comment'] = $class_ajax->convert_html_entities( $_POST['comment'] );

            $comment = $this->ipsclass->parse_clean_value( $_POST['comment'] );
            $comment = $class_ajax->ipsclass->txt_mbsubstr( $comment, 0, 400 );
            $comment = preg_replace( "#(\r\n|\r|\n|<br />|<br>){1,}#s", "\n", $comment );

            //-----------------------------------------
            // Bad words
            //-----------------------------------------

            $comment = trim( $this->parser->bad_words( $comment ) );

            // Added by BabyWolf to parse comment with emoticons
            $this->parser->parse_smilies 	= 1;
            $this->parser->parse_html    	= 0;
            $this->parser->parse_nl2br   	= 1;
            $this->parser->parse_bbcode  	= 0;
            $this->parser->bypass_badwords 	= intval($this->ipsclass->member['g_bypass_badwords']);

            $comment = $this->parser->pre_db_parse($comment);

            //-----------------------------------------
            // Got a comment?
            //-----------------------------------------

            if ( ! $comment )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error-no-comment';
                    exit();
    }

            //-----------------------------------------
            // Friend requires approval?
            //-----------------------------------------

            if ( $member['pp_setting_moderate_comments'] AND $member['id'] != $this->ipsclass->member['id'] )
            {
                    $comment_approved = 0;
            }

            //-----------------------------------------
            // Member is ignoring you!
            //-----------------------------------------

            if ( $comment_approved )
            { 
                    $_you_are_being_ignored = explode( ",", $member['ignored_users'] );

                    if ( is_array( $_you_are_being_ignored ) and count( $_you_are_being_ignored ) )
                    {
                            if ( in_array( $this->ipsclass->member['id'], $_you_are_being_ignored ) )
                            {
                                    $comment_approved = 0;
                            }
                    }
            }

            //-----------------------------------------
            // Add comment to the DB...
            //-----------------------------------------

            $this->ipsclass->DB->do_insert( 'profile_comments', array( 'comment_for_member_id' => $member_id,
                                                                                                                               'comment_by_member_id'  => $this->ipsclass->member['id'],
                                                                                                                               'comment_date'		   => time(),
                                                                                                                               'comment_ip_address'    => $this->ipsclass->ip_address,
                                                                                                                               'comment_approved'      => $comment_approved,
                                                                                                                               'comment_content'	   => $this->ipsclass->my_nl2br( $comment ) ) );

            $new_id = $this->ipsclass->DB->get_insert_id();

            //-----------------------------------------
            // What are we doing? Sending out 'mod' or 'yay'
            // message?
            //-----------------------------------------

            if ( ! $comment_approved AND $member['pp_setting_notify_comments'] AND ( $member['id'] != $this->ipsclass->member['id'] ) )
            {
                    $email->get_template("new_comment_request");

                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $member['members_display_name'],
                                                                              'COMMENT_NAME'         => $this->ipsclass->member['members_display_name'],
                                                                              'LINK'				 => $this->ipsclass->vars['board_url'] . '/index.' . $this->ipsclass->vars['php_ext'] . '?act=profile&CODE=personal_portal_view&tab=comments&id='.$member_id ) );

                    $message    = $email->message;
                    $subject    = $email->lang_subject;
                    $to         = $member;
                    $from       = $this->ipsclass->member;
                    $return_msg = 'pp_comment_added_mod';
            }
            else if ( $member['pp_setting_notify_comments'] AND ( $member['id'] != $this->ipsclass->member['id'] ) )
            {
                    $email->get_template("new_comment_added");

                    $email->build_message( array( 'MEMBERS_DISPLAY_NAME' => $member['members_display_name'],
                                                                              'COMMENT_NAME'         => $this->ipsclass->member['members_display_name'],
                                                                              'LINK'				 => $this->ipsclass->vars['board_url'] . '/index.' . $this->ipsclass->vars['php_ext'] . '?act=profile&CODE=personal_portal_view&tab=comments&id='.$member_id ) );

                    $message    = $email->message;
                    $subject    = $email->lang_subject;
                    $to         = $member;
                    $from       = $this->ipsclass->member;
                    $return_msg = '';
            }

            //-----------------------------------------
            // Got anything to send?
            //-----------------------------------------

            if ( $message AND $subject )
            {
                    //-----------------------------------------
                    // Email?
                    //-----------------------------------------

                    if ( $member['pp_setting_notify_comments'] == 'email' OR ( $member['pp_setting_notify_comments'] AND $member['members_disable_pm'] ) )
                    {
                            $email->subject = $subject;
                            $email->message = $message;
                            $email->to      = $to['email'];

                            $email->send_mail();
                    }

                    //-----------------------------------------
                    // PM?
                    //-----------------------------------------

                    else if ( $member['pp_setting_notify_comments'] != 'none' )
                    {
                            $msg_lib->to_by_id    = $to['id'];
                            $msg_lib->from_member = $from;
                            $msg_lib->msg_title   = $subject;

                            $msg_lib->postlib->parser->parse_bbcode 	= 1;
                            $msg_lib->postlib->parser->parse_smilies 	= 0;
                            $msg_lib->postlib->parser->parse_html 		= 0;
                            $msg_lib->postlib->parser->parse_nl2br 		= 1;
                            $msg_lib->msg_post    = $msg_lib->postlib->parser->pre_display_parse( $msg_lib->postlib->parser->pre_db_parse( $message ) );

                            $msg_lib->force_pm    = 1;

                            $msg_lib->send_pm();
                    }
            }

            //-----------------------------------------
            // Regenerate comments...
            //-----------------------------------------

            $class_ajax->return_html( $this->personal_build_comments( $member, $new_id, $return_msg ) );
            exit();
    }

    /*-------------------------------------------------------------------------*/
    // Load personal portal tab content
    /*-------------------------------------------------------------------------*/

    /**
    * Loads the content for the desired tab
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-08-02
    */
    function personal_ajax_load_tab()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id = intval( $this->ipsclass->input['member_id'] );
            $tab       = substr( $this->ipsclass->txt_alphanumerical_clean( str_replace( '..', '', trim( $this->ipsclass->input['tab'] ) ) ), 0, 20 );
            $md5check  = substr( $this->ipsclass->input['md5check'], 0, 32 );

            //-----------------------------------------
            // Load XML class
            //-----------------------------------------

            require_once( KERNEL_PATH . 'class_ajax.php' );
            $class_ajax           = new  class_ajax();
            $class_ajax->ipsclass =& $this->ipsclass;
            $class_ajax->class_init();

            //-----------------------------------------
            // MD5 check
            //-----------------------------------------

            if (  $md5check != $this->ipsclass->return_md5_check() )
    {
            @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
    }

            //-----------------------------------------
            // Load member
            //-----------------------------------------

            $member = $this->personal_function_load_member( $member_id );

            //-----------------------------------------
            // Check
            //-----------------------------------------

    if ( ! $member['id'] )
    {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
    }

            //-----------------------------------------
            // Load config
            //-----------------------------------------

            if( !file_exists( ROOT_PATH . 'sources/components_public/profile/'.$tab.'.conf.php' ) )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
            }

            require( ROOT_PATH . 'sources/components_public/profile/'.$tab.'.conf.php' );

            //-----------------------------------------
            // Active?
            //-----------------------------------------

            if ( ! $CONFIG['plugin_enabled'] )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
            }

            //-----------------------------------------
            // Load main class...
            //-----------------------------------------

            if( !file_exists( ROOT_PATH . 'sources/components_public/profile/'.$tab.'.php' ) )
            {
                    @header( "Content-type: text/plain" );
                    $class_ajax->print_nocache_headers();
                    print 'error';
                    exit();
            }

            require( ROOT_PATH . 'sources/components_public/profile/'.$tab.'.php' );
            $_func_name       = 'profile_'.$tab;
            $plugin           =  new $_func_name;
            $plugin->ipsclass =& $this->ipsclass;

            $html = $plugin->return_html_block( $member );

            //-----------------------------------------
            // Return it...
            //-----------------------------------------



            $class_ajax->return_html( $html );

            exit();

    }

    /*-------------------------------------------------------------------------*/
    // View personal portal
    /*-------------------------------------------------------------------------*/

    /**
    * Show's the personal portal for the member
    *
    *
    * @return	void
    * @since	IPB 2.2.0.2006-7-28 and most likely vB4.0.0-2006-x-x
    */
    function personal_portal_view()
    {
        $this->ipsclass->load_template('skin_regportal');
        $per_page = intval($this->ipsclass->vars['social_per_page_setting']) <=0 ? 25 :  $this->ipsclass->vars['social_per_page_setting'];
        $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
        $member_name = $this->ipsclass->input['MID']?trim($this->ipsclass->input['MID'],"/"):'';		
        $member_name  = $member_name != "" ? $member_name : $this->ipsclass->input['profile_name'];
        $member          = array();
        $comments        = array();
        $comments_html   = "";
        $friends         = array();
        $visitors        = array();
        $_member_ids     = array();

        if ( ! $member_name )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
            exit();
        }

        //-----------------------------------------
        // Grab all data...
        //-----------------------------------------
        // Get get member id by BabyWolf
        $this->ipsclass->DB->simple_construct(array('select'    => 'id',
                                                    'from'		=> 'members',
                                                    'where'		=> "name='".$member_name."'"
                                                    )
                                            );
        $this->ipsclass->DB->simple_exec();
        $result = $this->ipsclass->DB->fetch_row();
        if(!is_array($result)){
            $this->ipsclass->boink_it( $this->ipsclass->base_url);
            exit();
        }
        $member_id = $result['id'];
        $member = $this->personal_function_load_member( $member_id );
        // Prepare for building url by BabyWolf
        $member['uname'] = strtolower($this->ipsclass->textcode->RemoveAccent($member['name'],"-"));
        //-----------------------------------------
        // Got stuff?
        //-----------------------------------------

        if ( ! $member['id'] )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
        }
        $member = $this->common->personal_portal_set_information( $member );

        # Get random number from member's friend cache... grab 10 random. array_rand( array, no.)
        # also fall back on last 10 if no cache

        if ( $member['pp_setting_count_friends'] > 0 )
        {
//			

            $this->ipsclass->DB->build_query( array( 'select'   => 'pf.*',
                                                     'from'     => array( 'profile_friends' => 'pf' ),
                                                     'where'    => 'pf.friends_member_id='.$member_id. ' AND pf.friends_approved=1' . $sql_extra,
                                                     'limit'    => array( $start, $per_page ),
                                                     'add_join' => array( 0 => array( 'select' => 'm.*',
                                                                                                  'from'   => array( 'members' => 'm' ),
                                                                                                  'where'  => 'm.id=pf.friends_friend_id',
                                                                                                  'type'   => 'left' ),
                                                                          1 => array( 'select' => 'pp.*',
                                                                                                  'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                  'where'  => 'pp.pp_member_id=m.id',
                                                                                                  'type'   => 'left' ),
                                                                          2 => array( 'select' => 'me.*',
                                                                                                  'from'   => array( 'member_extra' => 'me' ),
                                                                                                  'where'  => 'me.id=pf.friends_friend_id',
                                                                                                  'type'   => 'left' ) ) ) );

            $this->ipsclass->DB->exec_query();

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                $row['_friends_added'] = $this->ipsclass->get_date( $row['friends_added'], 'SHORT' );
                $row['_avatar']        = $this->ipsclass->get_avatar( $row['avatar_location'], $this->ipsclass->member['view_avs'], $row['avatar_size'], $row['avatar_type'] );
                $row['_last_active']   = $this->ipsclass->get_date( $row['last_activity'], 'DATE' );
                $row['_location']	   = $row['location'] ? $row['location'] : $this->ipsclass->lang['no_info'];
                $row = $this->common->personal_portal_set_information( $row, 0, 0 );
                $row['members_display_name_short'] = $this->ipsclass->txt_truncate( $row['members_display_name'], 13 );
                $friends[] = $row;
            }
        }
        $member = $this->personal_portal_get_user_location( $member );
        $this->nav        = array( $this->ipsclass->lang['page_title_pp'] );
        $this->page_title = $member['members_display_name'] . ' - ' . $this->ipsclass->lang['page_title_pp'];	
        $comments[] = array();
        $datas = array();
        $friend_id = $this->ipsclass->input['friend_id'];
        $friends = $this->common->member_load_friends($member);
        $friends = (count($friends)<2)? null:array_slice($friends, 1, count($friends)-1);
        if(is_array($friends)){
            ksort( $friends );
        }
        if($this->ipsclass->member['id'] == "" || intval($this->ipsclass->member['id']) <= 0){
            $this->guest_view_profile();
            exit();
        }
        $datas = $this->common->member_load_comment($member, $per_page, $start, $this->ipsclass->member['id']);
        //list friend
        $stats = $this->ipsclass->DB->simple_exec_query( array( 'select' => 'count(pc.comment_id) as count',
                                                            'from'   => 'profile_comments pc',
                                                            'where'    => 'pc.comment_by_member_id='. intval($member['id']) . " or(pc.comment_for_member_id = " .intval($member['id']) . " and pc.comment_by_member_id = " .intval($this->ipsclass->member['id']) . " ) " 
                                                            ) 
                                                    );
        $pages = $this->common->create_customize_paging($stats['count'], $per_page, $start, "", $this->ipsclass->lang['tpl_pages'],"#&" . $key, TRUE, 'friend_comment_paging');
        $member['own'] = $member['id'];
        $member['member_name'] = $member['name'];
        $member['total_friends'] = count($friends);
        $member['approved_friends'] = $friends;	
        $comments['total_comments'] =  count($datas);
        $comments['datas'] = $datas;
        $comments['pages']=$pages;    
        $comments_html="";
        if(count($datas) > 0){
            $layout_comment = $this->ipsclass->compiled_templates['skin_regportal']->content_profile_comment($comments);	
            $member['layout_comment_display'] = "class='wrapper'";
        }
        //echo $layout_comment;exit();
        
        $member['layout_comment'] = $layout_comment;
        $member['tabs'] = $tabs;
        $member['comment_html'] = $comment_html;
        $member['friends'] = $friends;
        $member['visitors'] = $visitors;
        //$this->ipsclass->pageTitle = $this->ipsclass->lang['header_personal_info'];
        $is_own = $this->ipsclass->member['id'] == $member['id'] ? FALSE : TRUE;
        $this->output .= $this->common->load_left_content($member, $is_own, TRUE, TRUE, FALSE);
        $this->output .= $this->ipsclass->compiled_templates['skin_regportal']->skin_profile( $tabs, $member, $comment_html, $friends, $visitors, $tab, $tab_html );
    }
    function load_comments(){
        $this->ipsclass->load_template('skin_regportal');

        //-----------------------------------------
        // Check viewing permissions, etc
        //-----------------------------------------

        if ( ! $this->ipsclass->member['id'] )
        {
                $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_guests' ) );
        }
        $comments[] = array();
        $sql_extra       = '';
        $member_name = $this->ipsclass->input['MID']?trim($this->ipsclass->input['MID'],"/"):'';		
        $member_name  = $member_name != "" ? $member_name : $this->ipsclass->input['profile_name'];
        $this->ipsclass->DB->simple_construct(array('select'    => 'id',
                                                    'from'		=> 'members',
                                                    'where'		=> "name='".$member_name."'"
                                                    )
                                            );
        $this->ipsclass->DB->simple_exec();
        $result = $this->ipsclass->DB->fetch_row();
        $member_id = $result['id'];
        $member_id = $member_id !="" && intval($member_id)>0 ?$member_id : intval( $this->ipsclass->input['profile_id'] );	
        $per_page = intval($this->ipsclass->vars['social_per_page_setting']) <=0 ? 25 :  $this->ipsclass->vars['social_per_page_setting'];
        $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
        $member = $this->common->format_member($member_id);
        $datas = $this->member_load_comment($member,$this->ipsclass->member['id'], '', $per_page, $start);
        $stats = $this->ipsclass->DB->simple_exec_query( array( 'select' => 'count(pc.comment_id) as count',
                                                            'from'   => 'profile_comments pc',
                                                            'where'    => 'pc.comment_by_member_id='. intval($member['id']) . " or(pc.comment_for_member_id = " .intval($member['id']) . " and pc.comment_by_member_id = " .intval($this->ipsclass->member['id']) . " ) " 
                                                            ) 
                                                    );
        $pages = $this->common->create_customize_paging($stats['count'], $per_page, $start, "", $this->ipsclass->lang['tpl_pages'],"#" . $key, TRUE, 'friend_comment_paging');
        $comments['pages']=$pages;
        $comments['total_comments'] =  count($datas);
        $comments['datas'] = $datas;
        $layout_comment = $this->ipsclass->compiled_templates['skin_regportal']->content_profile_comment($comments);	
        echo $layout_comment;
        exit();
    }
    /**
    * Sets the personal photos up
    *
    * @return	void
    * @since	IPB 2.2.0.2006-7-31
    */
    function member_load_comment($member, $member_id="", $sql_extra = null, $per_page = "", $start = ""){
        
        //-----------------------------------------
    	// Check viewing permissions, etc
        //-----------------------------------------
        //echo $per_page;exit();
        if($per_page == "" || intval($per_page) <= 0){
            $per_page       = $this->ipsclass->vars['social_per_page_setting'];
        }
        if($this->ipsclass->input['st'] == "" || intval($this->ipsclass->input['st']) <= 0){
            $start = 0;
        }
        
        $this->ipsclass->DB->flush_query(); 
        $this->ipsclass->DB->build_query( array( 'select'   => 'pc.*',
                                                 'from'     => array( 'profile_comments' => 'pc' ),
                                                 'where'    => 'pc.comment_by_member_id='. intval($member['id']) . " or(pc.comment_for_member_id = " .intval($member['id']) . " and pc.comment_by_member_id = " .intval($member_id) . " ) " ,
                                                 
                                                 'limit'    => array( $start, $per_page ),
                                                 'order'    => 'pc.comment_date DESC',
                                                 'add_join' => array( 0 => array( 'select' => 'm.name, m.members_display_name, m.login_anonymous',
                                                                                  'from'   => array( 'members' => 'm' ),
                                                                                  'where'  => 'm.id=pc.comment_by_member_id',
                                                                                  'type'   => 'left' ),
                                                                      1 => array( 'select' => 'pp.*',
                                                                                  'from'   => array( 'profile_portal' => 'pp' ),
                                                                                  'where'  => 'pp.pp_member_id=m.id',
                                                                                  'type'   => 'left' ),	
                                                                      2 => array( 'select' => 'me.*',
                                                                                  'from'   => array( 'member_extra' => 'me' ),
                                                                                                                                                          'where'  => 'me.id=pc.comment_by_member_id',
                                                                                                                                                          'type'   => 'left' ) ) ) );


        $this->ipsclass->DB->exec_query();
        
        
        while( $row = $this->ipsclass->DB->fetch_row() )
        {
                $row['_comment_date']   = $this->ipsclass->get_date( $row['comment_date'], 'SHORT' );
                $row['_avatar']         = $this->ipsclass->get_avatar( $row['avatar_location'], $this->ipsclass->member['view_avs'], $row['avatar_size'], $row['avatar_type'] );
                $row['_last_active']    = $this->ipsclass->get_date( $row['last_activity'], 'SHORT' );
                // Added by BabyWolf
                $row['name'] 			= strtolower($row['name']);

                if( $row['login_anonymous']{0} == '1' )
                {
                        // Member last logged in anonymous

                        if( $this->ipsclass->member['mgroup'] != $this->ipsclass->vars['admin_group'] OR $this->ipsclass->vars['disable_admin_anon'] )
                        {
                                $row['_last_active'] = $this->ipsclass->lang['private'];
                        }
                }

                $row['comment_content'] = $this->ipsclass->txt_wordwrap( $row['comment_content'], '19', ' ' );

                $row = $this->personal_portal_set_information( $row, 0, 0 );

                $comments[] = $row;
        }
        //var_dump($comments);exit();
        return $comments;
    }
    /*-------------------------------------------------------------------------*/
    // Add recent visitor to a profile
    /*-------------------------------------------------------------------------*/

    /**
    * Adds a recent visitor to ones profile
    *
    * @return	void
    * @since	IPB 2.2.0.2006-7-31
    */
    function personal_portal_add_recent_visitor( $member=array(), $member_id_to_add=0 )
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $member_id_to_add = intval( $member_id_to_add );
            $found			  = 0;
            $_recent_visitors = array();

            //-----------------------------------------
            // Check...
            //-----------------------------------------

            if ( ! $member_id_to_add )
            {
                    return false;
            }

            //-----------------------------------------
            // Sort out data...
            //-----------------------------------------

            $recent_visitors = unserialize( $member['pp_last_visitors'] );

            if ( ! is_array( $recent_visitors ) OR ! count( $recent_visitors ) )
            {
                    $recent_visitors = array();
            }

            foreach( $recent_visitors as $_time => $_id )
            {
                    if ( $_id == $member_id_to_add )
                    {
                            $found  = 1;
                            continue;
                    }
                    else
                    {
                            $_recent_visitors[ $_time ] = $_id;
                    }
            }

            $recent_visitors = $_recent_visitors;

            krsort( $recent_visitors );

            //-----------------------------------------
            // Pop one off if we didn't update...
            //-----------------------------------------

            if ( ! $found )
            {
                    # Over 10? Pop one off...
                    if ( count( $recent_visitors ) > 13 )
                    {
                            $_tmp = array_pop( $recent_visitors );
                    }
            }

            # Add in ours..	
            $recent_visitors[ time() ] = $member_id_to_add;

            krsort( $recent_visitors );

            //-----------------------------------------
            // Update profile...
            //-----------------------------------------

            if ( $member['pp_member_id'] )
            {
                    $this->ipsclass->DB->do_update( 'profile_portal ', array( 'pp_last_visitors' => serialize( $recent_visitors ) ), 'pp_member_id='.$member['id'] );
            }
            else
            {
                    $this->ipsclass->DB->do_insert( 'profile_portal ', array( 'pp_member_id'      => $member['id'],
                                                                                                                                      'pp_profile_update' => time(),
                                                                                                                                      'pp_last_visitors' => serialize( $recent_visitors ) ) );
            }

            return true;
    }

    /*-------------------------------------------------------------------------*/
    // Set photos
    /*-------------------------------------------------------------------------*/

    /**
    * Sets the personal photos up
    *
    * @return	void
    * @since	IPB 2.2.0.2006-7-31
    */
    function personal_portal_set_information( $member, $noids=0, $use_parsed=1 )
    {
            return $this->ipsclass->member_set_information( $member, $noids, $use_parsed );
    }

    function personal_portal_get_user_location( $member=array() )
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $this->ipsclass->load_language( 'lang_online' );

    //-----------------------------------------
    // Build up our language hash
    //-----------------------------------------

            $where_lang = array();

    foreach ($this->ipsclass->lang as $k => $v)
    {
            if ( preg_match( "/^WHERE_(\w+)$/", $k, $match ) )
            {
                    $where_lang[ $match[1] ] = $this->ipsclass->lang[$k];
            }
    }

    unset($match);		

            $bypass_anon = 0;
            $our_mgroups = array();
            $where       = "";
            $cut_off     = ($this->ipsclass->vars['au_cutoff'] != "") ? $this->ipsclass->vars['au_cutoff'] * 60 : 900;
            $time_limit  = time() - $cut_off;

            list( $be_anon, $loggedin ) = explode( '&', $member['login_anonymous'] );
            $member['_online_location'] = '';

            //-----------------------------------------
            // Get other groups...
            //-----------------------------------------

            if ( $this->ipsclass->member['mgroup_others'] )
            {
                    $our_mgroups = explode( ",", $this->ipsclass->clean_perm_string( $this->ipsclass->member['mgroup_others'] ) );
            }

            $our_mgroups[] = $this->ipsclass->member['mgroup'];

            if ( (in_array( $this->ipsclass->vars['admin_group'], $our_mgroups )) and ($this->ipsclass->vars['disable_admin_anon'] != 1) )
            {
                    $bypass_anon = 1;
            }

            $member['sesslocation'] = strpos( $member['sesslocation'], "," ) ? strtolower( substr( $member['sesslocation'], 0, strpos( $member['sesslocation'], "," ) ) ) : $member['sesslocation'];

            //-----------------------------------------
            // DO it
            //-----------------------------------------

            if ( ( $member['last_visit'] > $time_limit or $member['last_activity'] > $time_limit ) AND $loggedin == 1 AND ( $be_anon != 1 OR $bypass_anon == 1 ) )
            {
                    //-----------------------------------------
                    // Module?
                    //-----------------------------------------

                    if ( strstr( $member['sesslocation'], 'mod:' ) )
                    {
                            $module = str_replace( 'mod:', '', $member['sesslocation'] );

                            $filename = ROOT_PATH.'sources/components_location/'.$this->ipsclass->txt_alphanumerical_clean( $module ).'.php';

                            if ( file_exists( $filename ) )
                            {
                                    $real_loc           = $member['location'];
                                    $member['location'] = $member['sesslocation'];

                                    require_once( $filename );
                                    $toload           =  'components_location_'.$module;
                                    $loader           =  new $toload;
                                    $loader->ipsclass =& $this->ipsclass;

                                    $tmp = $loader->parse_online_entries( array( 1 => $member ) );

                                    if ( is_array( $tmp ) and count( $tmp ) )
                                    {
                                            $where = "<a href='{$tmp[1]['_url']}'>{$tmp[1]['_text']}</a>";
                                    }

                                    $member['location'] = $real_loc;
                            }
                    }
                    else if( $member['sesslocation'] == 'post' )
                    {
                            if ( $member['location_1_type'] == 'topic' AND $member['location_1_id'] )
                            {
                                    // We have a topic id, must be a topic..

                                    $topic = $this->ipsclass->DB->simple_exec_query( array( 'select' => '*', 'from' => 'topics', 'where' => 'tid='.$member['location_1_id'] ) );

                                    if ( $topic['tid'] )
                                    {
                                            if ( ! $this->ipsclass->forums->forums_quick_check_access( $member['location_2_id'] ) )
                                            { 
                                                    $where = $this->ipsclass->lang['WHERE_postrep'].' '."<a href='{$this->ipsclass->base_url}showtopic={$topic['tid']}'>{$topic['title']}</a>";
                                            }
                                    }
                            }
                            else if( $member['location_2_type'] == 'forum' AND $member['location_2_id'] )
                            {
                                    if ( ! $this->ipsclass->forums->forums_quick_check_access( $member['location_2_id'] ) )
                                    {
                                            $where = $this->ipsclass->lang['WHERE_postnew'].' '."<a href='{$this->ipsclass->base_url}showforum={$this->ipsclass->cache['forum_cache'][ $member['location_2_id'] ]['id']}'>{$this->ipsclass->cache['forum_cache'][ $member['location_2_id'] ]['name']}</a>";
                                    }
                            }
                    }			
                    else if ( $member['location_1_type'] == 'topic' AND $member['location_1_id'] )
                    {
                            $topic = $this->ipsclass->DB->simple_exec_query( array( 'select' => '*', 'from' => 'topics', 'where' => 'tid='.$member['location_1_id'] ) );

                            if ( $topic['tid'] )
                            {
                                    if ( ! $this->ipsclass->forums->forums_quick_check_access( $member['location_2_id'] ) )
                                    { 
                                            $where = $this->ipsclass->lang['WHERE_st'].' '."<a href='{$this->ipsclass->base_url}showtopic={$topic['tid']}'>{$topic['title']}</a>";
                                    }
                            }
                    }
                    else if ( $member['location_2_type'] == 'forum' AND $member['location_2_id'] )
                    {
                            if ( ! $this->ipsclass->forums->forums_quick_check_access( $member['location_2_id'] ) )
                            {
                                    $where = $this->ipsclass->lang['WHERE_sf'].' '."<a href='{$this->ipsclass->base_url}showforum={$this->ipsclass->cache['forum_cache'][ $member['location_2_id'] ]['id']}'>{$this->ipsclass->cache['forum_cache'][ $member['location_2_id'] ]['name']}</a>";
                            }
                    }
                    else if( isset( $where_lang[$member['sesslocation']] ) AND $where_lang[$member['sesslocation']] )
                    {
                            if( in_array( $member['sesslocation'], array( 'members', 'help', 'calendar', 'online', 'boardrules' ) ) )
                            {
                                    $where = "<a href='{$this->ipsclass->base_url}act={$member['sesslocation']}'>{$where_lang[$member['sesslocation']]}</a>";
                            }
                            else
                            {
                                    $where = $where_lang[$member['sesslocation']];
                            }
                    }

                    if ( ! $where )
                    {
                            $where = "<a href='{$this->ipsclass->base_url}'>{$this->ipsclass->lang['board_index']}</a>";
                    }

                    $member['_online_location'] = $where;
            }

            return $member;
    }

    /*-------------------------------------------------------------------------*/
    // VIEW CONTACT CARD:
    /*-------------------------------------------------------------------------*/

    /**
    * Display member display name change history
    *
    * Prints a pop-up window of the member's display name
    * history
    *
    * @return	void
    * @since	IPB 2.1.0.2005-7-5
    */
    function show_display_names()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $id      = intval( $this->ipsclass->input['id'] );
            $member  = array();
            $html    = "";
            $content = "";

            //-----------------------------------------
            // Display name feature on?
            //-----------------------------------------

            if ( ! $this->ipsclass->vars['auth_allow_dnames'] )
            {
                    $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_permission' ) );
            }

            //-----------------------------------------
            // Permission check
            //-----------------------------------------

            if ( $this->ipsclass->member['g_mem_info'] != 1 )
            {
                    $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_permission' ) );
    }

            if ( ! $id )
    {
            $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'incorrect_use' ) );
    }

            //-----------------------------------------
            // Get member info
            //-----------------------------------------

    $this->ipsclass->DB->cache_add_query( 'generic_get_all_member', array( 'mid' => $id ) );
            $this->ipsclass->DB->cache_exec_query();

    $member = $this->ipsclass->DB->fetch_row();

    //-----------------------------------------
    // Get Dname history
    //-----------------------------------------

            $this->ipsclass->DB->build_query( array( 'select'   => 'd.*',
                                                                                             'from'     => array( 'dnames_change' => 'd' ),
                                                                                             'where'    => 'dname_member_id='.$id,
                                                                                             'add_join' => array( 0 => array( 'select' => 'm.members_display_name',
                                                                                                                                                              'from'   => array( 'members' => 'm' ),
                                                                                                                                                              'where'  => 'm.id=d.dname_member_id',
                                                                                                                                                              'type'   => 'inner' ) ),
                                                                                             'order'    => 'dname_date DESC' ) );
            $this->ipsclass->DB->exec_query();

    while( $row = $this->ipsclass->DB->fetch_row() )
    {
            //-----------------------------------------
            // Format some info
            //-----------------------------------------

            $date = $this->ipsclass->get_date( $row['dname_date'], 'SHORT' );

            //-----------------------------------------
            // Compile HTML
            //-----------------------------------------

            $content .= $this->ipsclass->compiled_templates['skin_profile']->dname_content_row( $row['dname_previous'], $row['dname_current'], $date );
    }

    //-----------------------------------------
    // No changes? Add in a default row
    //-----------------------------------------

    if ( ! $content )
    {
            $content .= $this->ipsclass->compiled_templates['skin_profile']->dname_content_row( '--', $member['members_display_name'], $this->ipsclass->get_date( $member['joined'], 'SHORT' ) );
    }

    //-----------------------------------------
    // Print the pop-up window
    //-----------------------------------------

    $html = $this->ipsclass->compiled_templates['skin_profile']->dname_wrapper( $member['members_display_name'], $content );

    $this->ipsclass->print->pop_up_window( $this->ipsclass->lang['dname_title'], $html );
}

    /*-------------------------------------------------------------------------*/
    // VIEW CONTACT CARD:
    /*-------------------------------------------------------------------------*/
    /**
    * @depricated	2006-08-16
    */
    function show_card()
    {
            $info = array();

            if ($this->ipsclass->member['g_mem_info'] != 1)
            {
                    $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_permission' ) );
    }

            //-----------------------------------------
    // Check input..
    //-----------------------------------------

    $id = intval($this->ipsclass->input['MID']);

    if ( empty($id) )
    {
            $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'incorrect_use' ) );
    }

    $this->ipsclass->DB->cache_add_query( 'generic_get_all_member', array( 'mid' => $id ) );
            $this->ipsclass->DB->cache_exec_query();

    $member = $this->ipsclass->DB->fetch_row();

    $member['website'] = $member['website'] == 'http://' ? '' : $member['website'];

    $info['aim_name']    = $member['aim_name']   ? $member['aim_name']   : $this->ipsclass->lang['no_info'];
    $info['icq_number']  = $member['icq_number'] ? $member['icq_number'] : $this->ipsclass->lang['no_info'];
    $info['yahoo']       = $member['yahoo']      ? $member['yahoo']      : $this->ipsclass->lang['no_info'];
    $info['location']    = $member['location']   ? $member['location']   : $this->ipsclass->lang['no_info'];
    $info['interests']   = $member['interests']  ? $member['interests']  : $this->ipsclass->lang['no_info'];
    $info['msn_name']    = $member['msnname']    ? $member['msnname']    : $this->ipsclass->lang['no_info'];
    $info['website']     = $member['website']    ? "<a href='{$member['website']}' target='_blank'>{$member['website']}</a>" : $this->ipsclass->lang['no_info'];
    $info['mid']         = $member['id'];
    $info['has_blog']    = $member['has_blog'];
    $info['has_gallery'] = isset($member['has_gallery']) ? $member['has_gallery'] : 0;

    if (!$member['hide_email'])
    {
                    $info['email'] = "<a href='javascript:redirect_to(\"&amp;act=Mail&amp;CODE=00&amp;MID={$member['id']}\",1);'>{$this->ipsclass->lang['click_here']}</a>";
            }
            else
            {
                    $info['email'] = $this->ipsclass->lang['private'];
            }

    $this->load_photo($id);

    if ( $this->show_photo )
    {
            $photo = $this->ipsclass->compiled_templates['skin_profile']->get_photo( $this->show_photo, $this->show_width, $this->show_height );
    }
    else
    {
            $photo = "<{NO_PHOTO}>";
    }

    if ( isset($this->ipsclass->input['download']) AND $this->ipsclass->input['download'] )
    {
            $photo = str_replace( "<{NO_PHOTO}>", $this->ipsclass->lang['no_photo_avail'], $photo );
            $html  = $this->ipsclass->compiled_templates['skin_profile']->show_card_download( $member['members_display_name'], $photo, $info );
            $html  = str_replace( "<!--CSS-->", $this->ipsclass->skin['_css'], $html );

            //-----------------------------------------
            // Macros
            //-----------------------------------------

            $macros = unserialize(stripslashes($this->ipsclass->skin['_macro']));

            if ( is_array( $macros ) )
                    {
                            foreach( $macros as $row )
                            {
                                    if ($row['macro_value'] != "")
                                    {
                                            $html = str_replace( "<{".$row['macro_value']."}>", $row['macro_replace'], $html );
                                    }
                            }
                    }

                    //-----------------------------------------
                    // Images
                    //-----------------------------------------

                    $html = str_replace( "<#IMG_DIR#>", $this->ipsclass->skin['_imagedir'], $html );

            if ( ! $this->ipsclass->vars['ipb_img_url'] )
                    {
                            $this->ipsclass->vars['ipb_img_url'] = preg_replace( "#/$#", "", $this->ipsclass->vars['board_url'] ) . '/';
                    }

                    $html = preg_replace( "#img\s+?src=[\"']style_(images|avatars|emoticons)(.+?)[\"'](.+?)?".">#is", "img src=\"".$this->ipsclass->vars['ipb_img_url']."style_\\1\\2\"\\3>", $html );

            //-----------------------------------------
            // Download
            //-----------------------------------------

                    @header("Content-type: unknown/unknown");
                    @header("Content-Disposition: attachment; filename={$member['members_display_name']}.html");
                    print $html;
                    exit();
    }
    else
    {
                    $html  = $this->ipsclass->compiled_templates['skin_profile']->show_card( $member['members_display_name'], $photo, $info );

                    $this->ipsclass->print->pop_up_window( $this->ipsclass->lang['photo_title'], $html );
    }
}

    /*-------------------------------------------------------------------------*/
    // VIEW PHOTO:
    /*-------------------------------------------------------------------------*/

    function show_photo()
    {
            //-----------------------------------------
            // INIT
            //-----------------------------------------

            $info = array();
            $id   = intval($this->ipsclass->input['MID']);

            if ($this->ipsclass->member['g_mem_info'] != 1)
            {
                    $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_permission' ) );
    }

            //-----------------------------------------
    // Check input..
    //-----------------------------------------

    if ( empty($id) )
    {
            $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'incorrect_use' ) );
    }

    $this->load_photo($id);

    $photo = $this->ipsclass->compiled_templates['skin_profile']->get_photo( $this->show_photo, $this->show_width, $this->show_height );
    $html  = $this->ipsclass->compiled_templates['skin_profile']->show_photo( $this->photo_member['members_display_name'], $photo );

    $this->ipsclass->print->pop_up_window( $this->ipsclass->lang['photo_title'], $html );
}
    
    /*-------------------------------------------------------------------------*/
    // FUNC: RETURN PHOTO
    /*-------------------------------------------------------------------------*/
    
    function load_photo($id, $member=array())
    {
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		$this->show_photo  = "";
    	$this->show_height = "";
    	$this->show_width  = "";
    	
    	if ( ! isset( $member['pp_member_id'] ) )
    	{
			$this->photo_member = $this->personal_function_load_member( $id );
    	}
    	else
    	{
    		$this->photo_member = $member;
    	}

		//-----------------------------------------
		// Set it up...
		//-----------------------------------------
		
		$this->photo_member = $this->personal_portal_set_information( $this->photo_member );
		
		//-----------------------------------------
		// Return...
		//-----------------------------------------
		
		$this->show_photo  = $this->photo_member['pp_main_photo'];
    	$this->show_width  = "width='"  . $this->photo_member['pp_main_width']  . "'";
		$this->show_height = "height='" . $this->photo_member['pp_main_height'] . "'";
    }
    
    /*-------------------------------------------------------------------------*/
    // VIEW MAIN PROFILE:
    /*-------------------------------------------------------------------------*/

    function view_profile()
    {
        //echo "dfdfdf";exit();
        $info = array();

        if ($this->ipsclass->member['g_mem_info'] != 1)
        {
                $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_permission' ) );
        }

            //-----------------------------------------
    // Check input..
    //-----------------------------------------

    $id = intval($this->ipsclass->input['MID']);

    if ( ! $id )
    {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
    }

    //-----------------------------------------
    // Get all member information
    //-----------------------------------------

    $this->ipsclass->DB->cache_add_query( 'profile_get_all', array( 'mid' => $id ) );

    $this->ipsclass->DB->cache_exec_query();

    $member = $this->ipsclass->DB->fetch_row();

    if ( empty( $member['id'] ) )
    {
            $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'incorrect_use' ) );
    }

    //-----------------------------------------
    // Most posted forum
    //-----------------------------------------

    $forum_ids = array('0');

    foreach( $this->ipsclass->forums->forum_by_id as $r )
    {
            $r['read_perms'] = isset($r['read_perms']) ? $r['read_perms'] : '';

            if ( $this->ipsclass->check_perms($r['read_perms']) == TRUE )
            {
                    $forum_ids[] = $r['id'];
            }
    }

    $this->ipsclass->DB->cache_add_query( 'profile_get_favourite', array( 'mid' => $member['id'], 'fid_array' => $forum_ids ) );

    $this->ipsclass->DB->cache_exec_query();

    $favourite   = $this->ipsclass->DB->fetch_row();

    //-----------------------------------------
    // Post count stats
    //-----------------------------------------

    $percent = 0;

    $this->ipsclass->DB->simple_construct( array( 'select' => 'COUNT(*) as total_posts',
                                                  'from'   => 'posts',
                                                  'where'  => "author_id={$member['id']}" ) );
            $this->ipsclass->DB->simple_exec();

    $total_posts = $this->ipsclass->DB->fetch_row();

    $board_posts = $this->ipsclass->cache['stats']['total_topics'] + $this->ipsclass->cache['stats']['total_replies'];

    if ($total_posts['total_posts'] > 0)
    {
            $percent = round( $favourite['f_posts'] / $total_posts['total_posts'] * 100 );
    }

    if ($member['posts'] and $board_posts)
    {
            $info['posts_day'] = round( $member['posts'] / (((time() - $member['joined']) / 86400)), 1);

            $info['total_pct'] = sprintf( '%.2f', ( $member['posts'] / $board_posts * 100 ) );
    }

    if ($info['posts_day'] > $member['posts'])
    {
            $info['posts_day'] = $member['posts'];
    }

    //-----------------------------------------
    // Pips / Icon
    //-----------------------------------------

    $pips = 0;

        foreach($this->ipsclass->cache['ranks'] as $k => $v)
        {
                if ($member['posts'] >= $v['POSTS'])
                {
                        if (!$member['title'])
                        {
                                $member['title'] = $this->ipsclass->cache['ranks'][ $k ]['TITLE'];
                        }

                        $pips = $v['PIPS'];
                        break;
                }
        }

        $member['member_rank_img'] = "";

        if ($this->ipsclass->cache['group_cache'][ $member['mgroup'] ]['g_icon'])
        {
                $member['member_rank_img'] = $this->ipsclass->compiled_templates['skin_profile']->member_rank_img($this->ipsclass->cache['group_cache'][ $member['mgroup'] ]['g_icon']);
        }
        else if ($pips)
        {
                if ( is_numeric( $pips ) )
                {
                        for ($i = 1; $i <= $pips; ++$i)
                        {
                                $member['member_rank_img'] .= "<{A_STAR}>";
                        }
                }
                else
                {
                        $member['member_rank_img'] = $this->ipsclass->compiled_templates['skin_profile']->member_rank_img('style_images/<#IMG_DIR#>/folder_team_icons/'.$pips);
                }
        }

    //-----------------------------------------
    // More info...
    //-----------------------------------------

    $info['posts']       = $member['posts'] ? $member['posts'] : 0;
    $info['name']        = $member['members_display_name'];
    $info['mid']         = $member['id'];

    if( isset($favourite['forum_id']) )
    {
        $info['fav_forum']   = $this->ipsclass->cache['forum_cache'][ $favourite['forum_id'] ]['name'] ? $this->ipsclass->cache['forum_cache'][ $favourite['forum_id'] ]['name'] : $this->ipsclass->lang['no_info'];
        }
        else
        {
            $info['fav_forum']	 = $this->ipsclass->lang['no_info'];
        }

    $info['fav_id']      = intval($favourite['forum_id']);
    $info['fav_posts']   = intval($favourite['f_posts']);
    $info['percent']     = $percent;
    $info['group_title'] = $this->ipsclass->cache['group_cache'][ $member['mgroup'] ]['g_title'];
    $info['board_posts'] = $board_posts;
    $info['joined']      = $this->ipsclass->get_date( $member['joined'], 'JOINED' );
    $info['last_active'] = $this->ipsclass->get_date( $member['last_activity'], 'SHORT' );

    if( $info['login_anonymous']{0} == '1' )
    {
            // Member last logged in anonymous

            if( $this->ipsclass->member['mgroup'] != $this->ipsclass->vars['admin_group'] OR $this->ipsclass->vars['disable_admin_anon'] )
            {
                    $info['last_active'] = $this->ipsclass->lang['private'];
            }
    }

    $info['member_title'] = $member['title']     ? $member['title']      : $this->ipsclass->lang['no_info'];

    $info['aim_name']             = $member['aim_name']   ? $member['aim_name']   : $this->ipsclass->lang['no_info'];
    $info['icq_number']           = $member['icq_number'] ? $member['icq_number'] : $this->ipsclass->lang['no_info'];
    $info['yahoo']                = $member['yahoo']      ? $member['yahoo']      : $this->ipsclass->lang['no_info'];
    $info['location']             = $member['location']   ? $member['location']   : $this->ipsclass->lang['no_info'];
    $info['interests']			  = $this->ipsclass->txt_wordwrap( $member['interests']  ? $member['interests']  : $this->ipsclass->lang['no_info'], '25', ' ' );
    $info['msn_name']             = $member['msnname']    ? $member['msnname']    : $this->ipsclass->lang['no_info'];
    $info['member_rank_img']      = $member['member_rank_img'];
    $info['has_blog']             = isset($member['has_blog']) ? $member['has_blog'] : 0;
    $info['has_gallery'] 		  = isset($member['has_gallery']) ? $member['has_gallery'] : 0;
    $info['members_display_name'] = $member['members_display_name'];

    //-----------------------------------------
    // Online, offline?
    //-----------------------------------------

    $cut_off = ($this->ipsclass->vars['au_cutoff'] != "") ? $this->ipsclass->vars['au_cutoff'] * 60 : 900;
    $time_limit    = time() - $cut_off;

    $info['online_status_indicator'] = '<{PB_USER_OFFLINE}>';
    $info['online_extra']            = '('.$this->ipsclass->lang['online_offline'].')';

    list( $be_anon, $loggedin ) = explode( '&', $member['login_anonymous'] );

    $bypass_anon = 0;

    $our_mgroups = array();

    if( $this->ipsclass->member['mgroup_others'] )
    {
            $our_mgroups = explode( ",", $this->ipsclass->clean_perm_string( $this->ipsclass->member['mgroup_others'] ) );
    }

    $our_mgroups[] = $this->ipsclass->member['mgroup'];

    if ( (in_array( $this->ipsclass->vars['admin_group'], $our_mgroups )) and ($this->ipsclass->vars['disable_admin_anon'] != 1) )
    {
            $bypass_anon = 1;
    }

    //-----------------------------------------
    // DO it
    //-----------------------------------------

    if ( ( $member['last_visit'] > $time_limit or $member['last_activity'] > $time_limit ) AND $loggedin == 1 AND ( $be_anon != 1 OR $bypass_anon == 1 ) )
    {
            $info['online_status_indicator'] = '<{PB_USER_ONLINE}>';

            //-----------------------------------------
            // Where?
            //-----------------------------------------

            $where = "";

            //-----------------------------------------
            // Module?
            //-----------------------------------------

            if ( strstr( $member['sesslocation'], 'mod:' ) )
            {
                    $module = str_replace( 'mod:', '', $member['sesslocation'] );

                    $filename = ROOT_PATH.'sources/components_location/'.$this->ipsclass->txt_alphanumerical_clean( $module ).'.php';

                    if ( file_exists( $filename ) )
                    {
                            $real_loc           = $member['location'];
                            $member['location'] = $member['sesslocation'];

                            require_once( $filename );
                            $toload           =  'components_location_'.$module;
                            $loader           =  new $toload;
                            $loader->ipsclass =& $this->ipsclass;

                            $tmp = $loader->parse_online_entries( array( 1 => $member ) );

                            if ( is_array( $tmp ) and count( $tmp ) )
                            {
                                    $where = "<a href='{$tmp[1]['_url']}'>{$tmp[1]['_text']}</a>";
                            }

                            $member['location'] = $real_loc;
                    }
            }

            else if ( $member['location_1_type'] == 'topic' AND $member['location_1_id'] )
            {
                    $topic = $this->ipsclass->DB->simple_exec_query( array( 'select' => '*', 'from' => 'topics', 'where' => 'tid='.$member['location_1_id'] ) );

                    if ( $topic['tid'] )
                    {
                            if ( ! $this->ipsclass->forums->forums_quick_check_access( $member['location_2_id'] ) )
                            { 
                                    $where = $this->ipsclass->lang['wol_topic'].': '."<a href='{$this->ipsclass->base_url}showtopic={$topic['tid']}'>{$topic['title']}</a>";
                            }
                    }
            }
            else if ( $member['location_2_type'] == 'forum' AND $member['location_2_id'] )
            {
                    if ( ! $this->ipsclass->forums->forums_quick_check_access( $member['location_2_id'] ) )
                    {
                            $where = $this->ipsclass->lang['wol_forum'].' '.$this->ipsclass->cache['forum_cache'][ $member['location_2_id'] ]['name'];
                    }
            }
            else if ( strstr( strtolower($member['sesslocation']), 'usercp' ) or strstr( strtolower($member['sesslocation']), 'msg' ) )
            {	
                    $where = $this->ipsclass->lang['wol_ucp'];
            }
            else if ( strstr( strtolower($member['sesslocation']), 'profile' )  )
            {	
                    $where = $this->ipsclass->lang['wol_profile'];
            }
            else if ( strstr( strtolower($member['sesslocation']), 'search' )  )
            {	
                    $where = $this->ipsclass->lang['wol_search'];
            }

            if ( ! $where )
            {
                    $where = $this->ipsclass->lang['wol_index'];
            }

            $info['online_extra'] = '('.$where.')';
    }

    //-----------------------------------------
    // Time...
    //-----------------------------------------

    $this->ipsclass->vars['time_adjust'] = $this->ipsclass->vars['time_adjust'] == "" ? 0 : $this->ipsclass->vars['time_adjust'];

    if ($member['dst_in_use'] == 1)
    {
            $member['time_offset'] += 1;
    }

    $info['local_time']  = $member['time_offset'] != "" ? gmdate( $this->ipsclass->vars['clock_long'], time() + ($member['time_offset']*3600) + ($this->ipsclass->vars['time_adjust'] * 60) ) : $this->ipsclass->lang['no_info'];

    $info['avatar']      = $this->ipsclass->get_avatar( $member['avatar_location'], $this->ipsclass->member['view_avs'], $member['avatar_size'], $member['avatar_type'] );

    //-----------------------------------------
    // Siggy
    //-----------------------------------------

    $info['signature']   = $member['signature'];

    //-----------------------------------------
    // site
    //-----------------------------------------

    if ( $member['website'] and preg_match( "/^http:\/\/\S+$/", $member['website'] ) )
    {
                    $info['homepage'] = "<a href='{$member['website']}' target='_blank'>{$member['website']}</a>";
            }
            else
            {
                    $info['homepage'] = $this->ipsclass->lang['no_info'];
            }

    //-----------------------------------------
    // Birthday
    //-----------------------------------------

    if ($member['bday_month'])
    {
            $info['birthday'] = $member['bday_day']." ".$this->ipsclass->lang[ 'M_'.$member['bday_month'] ]." ".$member['bday_year'];
    }
    else
    {
            $info['birthday'] = $this->ipsclass->lang['no_info'];
    }


    $this->page_title = $this->ipsclass->lang['page_title_pp'];
    $this->nav        = array( $this->ipsclass->lang['page_title_pp'] );
    }

    function get_month_name( $month=0 )
    {
            $month = intval($month);

            return ( isset($this->ipsclass->lang['M_'.$month]) ) ? $this->ipsclass->lang['M_'.$month] : false;
    }
    /*
     * Chung Nguyen Huu
     * own comment
     * 2011/09/12
     * Table: wg_profile_comments
     * column: comment_id:auto, comment_for_member_id (cua friend), comment_by_member_id(cua member),comment_date, comment_ip_address, comment_content, comment_approved
     */
    function owned_comment(){
        $this->ipsclass->load_template('skin_regportal');
        $comment_approved = 1;
        $comment = "";
        //$per_page = 1;
        $per_page = intval($this->ipsclass->vars['social_per_page_setting']) <=0 ? 25 :  $this->ipsclass->vars['social_per_page_setting'];
        $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
        if( empty ($this->ipsclass->input['txtmycomment']) ){
           echo "comment_empty";
           exit();
        }

        //-----------------------------------------
        // Load XML class
        //-----------------------------------------

        require_once( KERNEL_PATH . 'class_ajax.php' );
        $class_ajax           = new  class_ajax();
        $class_ajax->ipsclass =& $this->ipsclass;
        $class_ajax->class_init();
        /*
         * su ly du lieu truoc khi dua vao database
         * 
         */
        $comment = $class_ajax->convert_unicode( $this->ipsclass->input['txtmycomment'] );
        $comment = $class_ajax->convert_html_entities( $this->ipsclass->input['txtmycomment'] );
        $for_id= intval($this->ipsclass->input['own_id']);
        //$comment = $this->ipsclass->parse_clean_value( $this->ipsclass->input['txtmycomment'] );
        $comment = $class_ajax->ipsclass->txt_mbsubstr( $comment, 0, 400 );
        $comment = preg_replace( "#(\r\n|\r|\n|<br />|<br>){1,}#s", "\n", $comment );

        //-----------------------------------------
        // Bad words
        //-----------------------------------------


        // Added by BabyWolf to parse comment with emoticons

        $this->parser->parse_smilies 	= 1;
        $this->parser->parse_html    	= 0;
        $this->parser->parse_nl2br   	= 1;
        $this->parser->parse_bbcode  	= 0;
        $this->parser->bypass_badwords 	= intval($this->ipsclass->member['g_bypass_badwords']);
        $this->ipsclass->DB->do_insert( 'profile_comments', array( 'comment_for_member_id' => $for_id,
                                                                   'comment_by_member_id'  => $this->ipsclass->member['id'],
                                                                   'comment_date'          => time(),
                                                                   'comment_ip_address'    => $this->ipsclass->ip_address,
                                                                   'comment_approved'      => $comment_approved,
                                                                   'comment_content'       => $this->ipsclass->my_nl2br( $comment ) ) );
        //lay ra id cua comment vua insert
        //kiem tra va thong bao den client

    //-----------------------------------------
    // Regenerate comments... by tuanhnm
    //-----------------------------------------
    $stats = $this->ipsclass->DB->simple_exec_query( array( 'select' => 'count(pc.comment_id) as count',
                                                                'from'   => 'profile_comments pc',
                                                                'where'    => 'pc.comment_by_member_id='. intval($for_id) . " or(pc.comment_for_member_id = " .intval($for_id) . " and pc.comment_by_member_id = " .intval($this->ipsclass->member['id']) . " ) " 
                                                                ) 
                                                        );

    /*
     * Paging
     */
//    $pages = $this->ipsclass->build_pagelinks( array(  'TOTAL_POSS'  => $stats['count'],
//                                                       'PER_PAGE'    => $per_page,
//                                                       'CUR_ST_VAL'  => $start,
//                                                       'L_SINGLE'    => "",
//                                                       'L_MULTI'     => $this->ipsclass->lang['tpl_pages'],
//                                                       'BASE_URL'    => "#"
//                                                    )
//                                                );
    $pages = $this->common->create_customize_paging($stats['count'], $per_page, $start, "", $this->ipsclass->lang['tpl_pages'],'#', TRUE, 'friend_comment_paging');
    $comments['pages']=$pages;
    $comments[] = array();
    $member = $this->common->format_member($for_id);
//    $member = $this->personal_function_load_member( $for_id );	
//    $member = $this->personal_portal_set_information($member);
    $datas = $this->common->member_load_comment($member, $per_page, $start, $this->ipsclass->member['id']);
    $comments['total_comments'] =  count($datas);
    $comments['datas'] = $datas;
    $layout_comment = $this->ipsclass->compiled_templates['skin_regportal']->content_profile_comment($comments);	
    //	
    $this->nav        = array( $this->ipsclass->lang['page_title_pp'] );
    $this->page_title = $member['members_display_name'] . ' - ' . $this->ipsclass->lang['page_title_pp'];	
    echo "own_comment_success[$$$$$]" . $layout_comment;
    unset ($comment);
    unset($layout_comment);
    exit();
    }

    function delete_owned_comment(){
        //exit();
        $this->ipsclass->load_template('skin_regportal');
        $comment_id = "";
        $comment_for_member_id = "";
        $comment_by_member_id = "";
        $per_page       = $this->ipsclass->vars['social_per_page_setting'];
        $start          = intval( $this->ipsclass->input['st'] ) >= 0 ? intval( $this->ipsclass->input['st'] ) : 0;
        //lay id tu client
        $id_str = $this->ipsclass->input['id'];
        //echo $id_str;exit();
        $ids = explode("_", $id_str);

        if(count($ids) != 3){
            echo "own_delete_param_invalid";
            exit();
        }
        $comment_id = intval($ids[0]);
        $comment_by_member_id = intval($ids[1]);
        $comment_for_member_id = intval($ids[2]);
        
        if($this->ipsclass->member['g_is_supmod']){
            $this->ipsclass->DB->build_and_exec_query( array( 'delete' => 'profile_comments',
                                                              'where'  => 'comment_id='.$comment_id ) );
            echo "own_delete_comment_success[$$$$$]".$layout_comment;
            exit();
        }
        if($comment_by_member_id != $this->ipsclass->member['id']){
            echo "own_delete_access_deny";
            exit();
        }
        //echo $comment_by_member_id . "@" . $comment_for_member_id;exit();

        if($comment_by_member_id == $this->ipsclass->member['id']){
            $this->ipsclass->DB->build_and_exec_query( array( 'delete' => 'profile_comments',
                                                              'where'  => 'comment_id='.$comment_id ) );
        }
        //-----------------------------------------
        // Regenerate comments... by tuanhnm
        //-----------------------------------------

//        $sql_extra = '';
//        $comments[] = array();
//        $member = $this->common->format_member($comment_by_member_id);
//        $member = $this->common->personal_function_load_member( $comment_by_member_id );	
//        $member = $this->common->personal_portal_set_information($member);
//            $datas = $this->member_load_comment($member,$this->ipsclass->member['id'], '', $per_page, $start);
//        $stats = $this->ipsclass->DB->simple_exec_query( array( 'select' => 'count(pc.comment_id) as count',
//                                                                    'from'   => 'profile_comments pc',
//                                                                    'where'    => 'pc.comment_by_member_id='. intval($member['id']) . " or(pc.comment_for_member_id = " .intval($member['id']) . " and pc.comment_by_member_id = " .intval($this->ipsclass->member['id']) . " ) " 
//                                                                    ) 
//                                                            );
//
//        /*
//         * Paging
//         */
//        $pages = $this->ipsclass->build_pagelinks( array(  'TOTAL_POSS'  => $stats['count'],
//                                                           'PER_PAGE'    => $per_page,
//                                                           'CUR_ST_VAL'  => $start,
//                                                           'L_SINGLE'    => "",
//                                                           'L_MULTI'     => $this->ipsclass->lang['tpl_pages'],
//                                                           'BASE_URL'    => "#"
//                                                        )
//                                                    );
//        $comments['pages']=$pages;
//        $comments['total_comments'] =  count($datas);
//        $comments['datas'] = $datas;
//        $layout_comment = $this->ipsclass->compiled_templates['skin_regportal']->content_profile_comment($comments);	
        echo "own_delete_comment_success[$$$$$]".$layout_comment;
        exit();
    }
    /*
     * Chung nguyen huu
     * friends by id
     * Date: 2011/09/14
     */
    function member_load_friends($member, $clause = NULL, $per_page = "", $start = ""){
        if(empty ($member['id'])){
            echo "author_fail";
            exit();
        }

        $per_page = intval($this->ipsclass->vars['social_per_page_setting']) <=0 ? 25 :  $this->ipsclass->vars['social_per_page_setting'];
        $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
        $friends[] = array();
        $query = ' ( pf.friends_member_id = '.intval($member['id']) . ' or pf.friends_friend_id = ' . intval($member['id']) .')  AND pf.friends_approved = 1 AND  m.id <> ' . intval($member['id']);

        $this->ipsclass->DB->flush_query();
        $this->ipsclass->DB->build_query( array( 'select'   => ' pf.*',
                                                 'from'     => array( 'profile_friends' => 'pf' ),
                                                 'where'    => $query . ' group by concat(m.id) ',
                                                 'limit'         =>array($start, $per_page),
                                                 'add_join' => array(0 => array( 'select' => 'm.*',
                                                                    'from'   => array( 'members' => 'm' ),
                                                                    'where'  => 'm.id=pf.friends_friend_id or  m.id=pf.friends_member_id ',
                                                                    'type'   => 'left' ),  
                                                                    1 => array( 'select' => 'me.*',
                                                                      'from'   => array( 'member_extra' => 'me' ),
                                                                      'where'  => 'me.id=m.id',
                                                                      'type'   => 'left' ),
                                                                    2 => array( 'select' => 'pp.*',
                                                                      'from'   => array( 'profile_portal' => 'pp' ),
                                                                      'where'  => 'pp.pp_member_id=m.id',
                                                                  'type'   => 'left' ),
                                                                     3 => array( 'select' => 'pc.*',
                                                                                      'from'   => array( 'pfields_content' => 'pc' ),
                                                                                      'where'  => 'pc.member_id=m.id',
                                                                                      'type'   => 'left' ) 
                                                     )
                                                )
                    );

            $this->ipsclass->DB->exec_query();	

            while($row = $this->ipsclass->DB->fetch_row()){
                //$datas[] = $this->ipsclass->member_set_information($row);
                $friends[] = $this->ipsclass->member_set_information($row);
            }	     
        unset ($member);

        return $friends;
    }
    function load_all_friends(){
        //load skin
        //echo "dfdfdf";exit();
        $this->ipsclass->load_template('skin_regportal');
        $id = $this->ipsclass->input['friend_id'];
        $per_page = intval($this->ipsclass->vars['social_per_page_setting']) <=0 ? 25 :  $this->ipsclass->vars['social_per_page_setting'];
        //$per_page = 1;
        $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
        //echo $start;
        $friends[] = array();
        $datas = array();

        $query = ' ( pf.friends_member_id = '.$id . ' or pf.friends_friend_id = ' . intval($id) .')  AND pf.friends_approved = 1 AND  m.id <> ' . intval($id) ;

        $this->ipsclass->DB->flush_query();
        $this->ipsclass->DB->build_query( array( 'select'   => ' pf.*',
                                                 'from'     => array( 'profile_friends' => 'pf' ),
                                                 'where'    => $query . ' group by concat( m.id )',
                                                 'limit'         =>array($start, $per_page),
                                                 'add_join' => array(0 => array( 'select' => 'm.*',
                                                                    'from'   => array( 'members' => 'm' ),
                                                                    'where'  => 'm.id=pf.friends_friend_id or  m.id=pf.friends_member_id ',
                                                                    'type'   => 'left' ),  
                                                                    1 => array( 'select' => 'me.*',
                                                                      'from'   => array( 'member_extra' => 'me' ),
                                                                      'where'  => 'me.id=m.id',
                                                                      'type'   => 'left' ),
                                                                    2 => array( 'select' => 'pp.*',
                                                                      'from'   => array( 'profile_portal' => 'pp' ),
                                                                      'where'  => 'pp.pp_member_id=m.id',
                                                                  'type'   => 'left' ),
                                                                     3 => array( 'select' => 'pc.*',
                                                                                      'from'   => array( 'pfields_content' => 'pc' ),
                                                                                      'where'  => 'pc.member_id=m.id',
                                                                                      'type'   => 'left' ) 
                                                     )
                                                )
                    );
            $this->ipsclass->DB->exec_query();	

        while($row = $this->ipsclass->DB->fetch_row()){
            $datas[] = $this->ipsclass->member_set_information($row);
        }
        if(is_array($datas)){
            ksort($datas);
        }

        
        $stats = $this->ipsclass->DB->simple_exec_query( array( 'select' => 'count(pf.friends_id) as count',
                                                                'from'   => 'profile_friends pf',
                                                                'where'    => '( pf.friends_member_id = ' .$id .' or pf.friends_member_id = '.$id . ')'
                                                                ) 
                                                        );
        $pages = $this->common->create_customize_paging($stats['count'], $per_page, $start, "", $this->ipsclass->lang['tpl_pages'],'#', TRUE, "all_friend_paging");
        /*
         * Paging
         */
        $this->nav        = array( $this->ipsclass->lang['page_title_pp'] );
        $this->page_title = $member['members_display_name'] . ' - ' . $this->ipsclass->lang['page_title_pp'];	
        $friends['pages']=$pages;
        $friends['total_friends'] = count($datas);
        $friends['owned_id'] = $this->ipsclass->member['id'];
        $friends['datas'] = $datas;
        $layout_friends = $this->ipsclass->compiled_templates['skin_regportal']->content_profile_load_all_friends($friends);	
        echo $layout_friends;
        exit();
    }

    function guest_view_profile(){

        $this->ipsclass->load_template('skin_regportal');

        //-----------------------------------------
        // INIT
        //-----------------------------------------

        $member_name = $this->ipsclass->input['MID']?trim($this->ipsclass->input['MID'],"/"):'';		
        $member_name  = $member_name != "" ? $member_name : $this->ipsclass->input['profile_name'];
        //echo  $member_name;exit();

        $member          = array();
        $friends         = array();
        $visitors        = array();
        $_member_ids     = array();
        $sql_extra       = '';
        if ( ! $member_name )
        {
                $this->ipsclass->boink_it( $this->ipsclass->base_url );
                exit();
        }

        //-----------------------------------------
        // Grab all data...
        //-----------------------------------------
        // Get get member id by BabyWolf
        $this->ipsclass->DB->simple_construct(array('select'    => 'id',
                                                    'from'		=> 'members',
                                                    'where'		=> "name='".$member_name."'"
                                                    )
                                            );
        $this->ipsclass->DB->simple_exec();
        $result = $this->ipsclass->DB->fetch_row();
        $member_id = $result['id'];


        //var_dump($member_id);exit();

        $member = $this->common->personal_function_load_member( $member_id );
        //var_dump($member['joined']);exit();
        // Prepare for building url by BabyWolf
        $member['uname'] = strtolower($this->ipsclass->textcode->RemoveAccent($member['name'],"-"));
        //-----------------------------------------
        // Got stuff?
        //-----------------------------------------

        if ( ! $member['id'] )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
        }
        $member = $this->common->personal_portal_set_information( $member );

        # Get random number from member's friend cache... grab 10 random. array_rand( array, no.)
        # also fall back on last 10 if no cache

        if ( $member['pp_setting_count_friends'] > 0 ){
            $this->ipsclass->DB->build_query( array( 'select'   => 'pf.*',
                                                     'from'     => array( 'profile_friends' => 'pf' ),
                                                     'where'    => 'pf.friends_member_id='.$member_id. ' AND pf.friends_approved=1' . $sql_extra,
                                                     'limit'    => array( $start, $per_page ),
                                                     'add_join' => array( 0 => array( 'select' => 'm.*',
                                                                                                  'from'   => array( 'members' => 'm' ),
                                                                                                  'where'  => 'm.id=pf.friends_friend_id',
                                                                                                  'type'   => 'left' ),
                                                                          1 => array( 'select' => 'pp.*',
                                                                                                  'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                  'where'  => 'pp.pp_member_id=m.id',
                                                                                                  'type'   => 'left' ),
                                                                          2 => array( 'select' => 'me.*',
                                                                                                  'from'   => array( 'member_extra' => 'me' ),
                                                                                                  'where'  => 'me.id=pf.friends_friend_id',
                                                                                                  'type'   => 'left' ) ) ) );

            $this->ipsclass->DB->exec_query();

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                $row['_friends_added'] = $this->ipsclass->get_date( $row['friends_added'], 'SHORT' );
                $row['_avatar']        = $this->ipsclass->get_avatar( $row['avatar_location'], $this->ipsclass->member['view_avs'], $row['avatar_size'], $row['avatar_type'] );
                $row['_last_active']   = $this->ipsclass->get_date( $row['last_activity'], 'DATE' );
                $row['_location']	   = $row['location'] ? $row['location'] : $this->ipsclass->lang['no_info'];
                $row = $this->common->personal_portal_set_information( $row, 0, 0 );
                $row['members_display_name_short'] = $this->ipsclass->txt_truncate( $row['members_display_name'], 13 );
                $friends[] = $row;
            }
        }
        $member = $this->personal_portal_get_user_location( $member );
        //var_dump($member);exit();
        //-----------------------------------------
        // Add to output
        //-----------------------------------------



        $this->nav        = array( $this->ipsclass->lang['page_title_pp'] );
        $this->page_title = $member['members_display_name'] . ' - ' . $this->ipsclass->lang['page_title_pp'];	
        $comments[] = array();
        $datas = array();
        $friend_id = $this->ipsclass->input['friend_id'];




        //echo $pages;exit();

        $friends = $this->common->member_load_friends($member);
        //load friends
        $friends = (count($friends)<2)? null:array_slice($friends, 1, count($friends)-1);
        if(is_array($friends)){
            ksort( $friends );
        }

        /*
         * Total invite friend
         */
        //$total_invited = $regportal->count("profile_friends", "friends_id", "friends_approved = 0 and friends_member_id = ". $member['id']). "/" . $regportal->count("profile_friends", "friends_id", "friends_member_id = ". $member['id']);
        $total_messages = $this->common->count_messages($member);
        $total_invited = $this->common->count_invited($member_id);
        $comment_html = $this->ipsclass->compiled_templates['skin_regportal']-> guest_view_profile($member);	
        //var_dump($comment_html);exit();
        // get total friends of a current user
        $member['own'] = $member['id'];
        $member['member_name'] = $member['name'];
        $member['total_invited'] = $total_invited;
        $member['total_friends'] = count($friends);
        $member['approved_friends'] = $friends;	

        $member['friends'] = $friends;
        $member['visitors'] = $visitors;
        $this->output .= $this->common->load_left_content($member, FALSE, TRUE, TRUE, FALSE);
        $this->output .= $this->ipsclass->compiled_templates['skin_regportal']->skin_guest_view_profile( $member, $comment_html, $friends, $visitors);
        //echo $this->output;
        //exit();
    }
    function count($table_name, $column_count_name, $clause = null) {
            if (!empty($clause)) {
                $clause .= " and ";
            }
            $this->ipsclass->DB->flush_query();
            $this->ipsclass->DB->build_query(array('select' => 'count(' . $column_count_name . ') as total',
            'from' => $table_name,
            'where' => $clause . ' 1 = 1; ')
        );
        $this->ipsclass->DB->exec_query();
        $row = $this->ipsclass->DB->fetch_row();
        return $row['total'];
        //return 0;
    }
    function load_diaries(){
        $this->ipsclass->load_template('skin_regportal');
        //$per_page = 1;
        
        $per_page = intval($this->ipsclass->vars['social_per_page_setting']) <=0 ? 25 :  $this->ipsclass->vars['social_per_page_setting'];
        $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
        $member_name = $this->ipsclass->input['MID']?trim($this->ipsclass->input['MID'],"/"):'';		
        $member_name  = $member_name != "" ? $member_name : $this->ipsclass->input['profile_name'];
        //echo  $member_name;exit();
//        $tab             = substr( $this->ipsclass->txt_alphanumerical_clean( str_replace( '..', '', trim( $this->ipsclass->input['tab'] ) ) ), 0, 20 );
//        $tab             = $tab ? $tab : 'comments'; // Modified from 'topics' to 'comments' by BabyWolf
        $member          = array();
//        $comments        = array();
//        $comments_html   = "";
//        $friends         = array();
//        $visitors        = array();
//        $comment_perpage = $this->ipsclass->vars['social_per_page_setting'];
//        $pips            = 0;
//        $tabs            = array();
//        $_tabs           = array();
//        $_positions      = array( 0 => 0 );
//        $custom_path     = ROOT_PATH . 'sources/components_public/profile';
//        $_member_ids     = array();
//        $sql_extra       = '';
//        $pass            = 0;
//        $mod             = 0;

        if ( ! $member_name )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
            exit();
        }

        //-----------------------------------------
        // Grab all data...
        //-----------------------------------------
        // Get get member id by BabyWolf
        $this->ipsclass->DB->simple_construct(array('select'    => 'id',
                                                    'from'		=> 'members',
                                                    'where'		=> "name='".$member_name."'"
                                                    )
                                            );
        $this->ipsclass->DB->simple_exec();
        $result = $this->ipsclass->DB->fetch_row();
        $member_id = $result['id'];

        $member = $this->common->personal_function_load_member( $member_id );

        // Prepare for building url by BabyWolf
        $member['uname'] = strtolower($this->ipsclass->textcode->RemoveAccent($member['name'],"-"));
        //-----------------------------------------
        // Got stuff?
        //-----------------------------------------

        if ( ! $member['id'] )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
        }
        $member = $this->common->personal_portal_set_information( $member );

        # Get random number from member's friend cache... grab 10 random. array_rand( array, no.)
        # also fall back on last 10 if no cache

        if ( $member['pp_setting_count_friends'] > 0 )
        {
//			

            $this->ipsclass->DB->build_query( array( 'select'   => 'pf.*',
                                                     'from'     => array( 'profile_friends' => 'pf' ),
                                                     'where'    => 'pf.friends_member_id='.$member_id. ' AND pf.friends_approved=1' . $sql_extra,
                                                     'limit'    => array( $start, $per_page ),
                                                     'add_join' => array( 0 => array( 'select' => 'm.*',
                                                                                                  'from'   => array( 'members' => 'm' ),
                                                                                                  'where'  => 'm.id=pf.friends_friend_id',
                                                                                                  'type'   => 'left' ),
                                                                          1 => array( 'select' => 'pp.*',
                                                                                                  'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                  'where'  => 'pp.pp_member_id=m.id',
                                                                                                  'type'   => 'left' ),
                                                                          2 => array( 'select' => 'me.*',
                                                                                                  'from'   => array( 'member_extra' => 'me' ),
                                                                                                  'where'  => 'me.id=pf.friends_friend_id',
                                                                                                  'type'   => 'left' ) ) ) );

            $this->ipsclass->DB->exec_query();

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                $row['_friends_added'] = $this->ipsclass->get_date( $row['friends_added'], 'SHORT' );
                $row['_avatar']        = $this->ipsclass->get_avatar( $row['avatar_location'], $this->ipsclass->member['view_avs'], $row['avatar_size'], $row['avatar_type'] );
                $row['_last_active']   = $this->ipsclass->get_date( $row['last_activity'], 'DATE' );
                $row['_location']	   = $row['location'] ? $row['location'] : $this->ipsclass->lang['no_info'];
                $row = $this->common->personal_portal_set_information( $row, 0, 0 );
                $row['members_display_name_short'] = $this->ipsclass->txt_truncate( $row['members_display_name'], 13 );

                $friends[] = $row;
            }
        }
        $member = $this->personal_portal_get_user_location( $member );

        //-----------------------------------------
        // Add to output
        //-----------------------------------------
        $this->nav        = array( $this->ipsclass->lang['page_title_pp'] );
        $this->page_title = $member['members_display_name'] . ' - ' . $this->ipsclass->lang['page_title_pp'];	
        /*
         * Total invite friend
         */
        $total_messages = $this->common->count_messages($member);
        $total_invited = $this->common->count_invited($member['id']);
        if($this->ipsclass->member['id'] == "" || intval($this->ipsclass->member['id']) <= 0){
            $this->guest_view_profile();
            exit();
        }
        $stats = $this->ipsclass->DB->simple_exec_query( array( 'select' => 'count(pc.comment_id) as count',
                                                            'from'   => 'profile_comments pc',
                                                            'where'    => 'pc.comment_by_member_id='. intval($member['id']) . " or(pc.comment_for_member_id = " .intval($member['id']) . " and pc.comment_by_member_id = " .intval($this->ipsclass->member['id']) . " ) " 
                                                            ) 
                                                    );
        /*
         * Paging
         */
        $pages = $this->common->create_customize_paging($stats['count'], $per_page, $start, "", $this->ipsclass->lang['tpl_pages'],'#'.$key, TRUE, 'diaries_paging');
        $member['own'] = $member['id'];
        $member['member_name'] = $member['name'];
        $member['total_invited'] = $total_invited;
//        $member['total_friends'] = count($friends);
        $member['approved_friends'] = $friends;	
//        $member['friends'] = $friends;
//        $member['visitors'] = $visitors;
        require_once( ROOT_PATH . 'sources/action_public/diary.php' );
        $diary = new diary();
        $diary->ipsclass = & $this->ipsclass;
        
        $diary_layout = $diary->get_diaries($per_page, $start);
        //echo $diary_layout;
        //echo $this->ipsclass->compiled_templates['skin_regportal']->skin_profile_diaries( $member, $diary_layout, $friends, $visitors);exit();
        $this->output .= $this->common->load_left_content($member, FALSE, TRUE, TRUE, FALSE);
        
        $this->output .= $this->ipsclass->compiled_templates['skin_regportal']->skin_profile_diaries( $member, $diary_layout, $friends, $visitors);
        //$this->output .= $this->common->load_right_content();
        
            
    }
    function friend_profile(){
        $this->ipsclass->load_template('skin_regportal');
        $this->ipsclass->load_language('lang_regportal');
        $member_id = $this->ipsclass->input['id'];
        $this->ipsclass->DB->simple_construct(array('select'    => 'id',
                                                    'from'		=> 'members',
                                                    'where'		=> "id={$member_id}"
                                                    )
                                            );
        $this->ipsclass->DB->simple_exec();
        $result = $this->ipsclass->DB->fetch_row();
        $member_id = $result['id'];
        if(intval($member_id) <= 0){
            return null;
        }
        if ( $member['pp_setting_count_friends'] > 0 ){
            $this->ipsclass->DB->build_query( array( 'select'   => 'pf.*',
                                                     'from'     => array( 'profile_friends' => 'pf' ),
                                                     'where'    => 'pf.friends_member_id='.$member_id. ' AND pf.friends_approved=1' . $sql_extra,
                                                     'limit'    => array( $start, $per_page ),
                                                     'add_join' => array( 0 => array( 'select' => 'm.*',
                                                                                                  'from'   => array( 'members' => 'm' ),
                                                                                                  'where'  => 'm.id=pf.friends_friend_id',
                                                                                                  'type'   => 'left' ),
                                                                          1 => array( 'select' => 'pp.*',
                                                                                                  'from'   => array( 'profile_portal' => 'pp' ),
                                                                                                  'where'  => 'pp.pp_member_id=m.id',
                                                                                                  'type'   => 'left' ),
                                                                          2 => array( 'select' => 'me.*',
                                                                                                  'from'   => array( 'member_extra' => 'me' ),
                                                                                                  'where'  => 'me.id=pf.friends_friend_id',
                                                                                                  'type'   => 'left' ) ) ) );

            $this->ipsclass->DB->exec_query();

            while( $row = $this->ipsclass->DB->fetch_row() )
            {
                $row['_friends_added'] = $this->ipsclass->get_date( $row['friends_added'], 'SHORT' );
                $row['_avatar']        = $this->ipsclass->get_avatar( $row['avatar_location'], $this->ipsclass->member['view_avs'], $row['avatar_size'], $row['avatar_type'] );
                $row['_last_active']   = $this->ipsclass->get_date( $row['last_activity'], 'DATE' );
                $row['_location']	   = $row['location'] ? $row['location'] : $this->ipsclass->lang['no_info'];
                $row = $this->common->personal_portal_set_information( $row, 0, 0 );
                $row['members_display_name_short'] = $this->ipsclass->txt_truncate( $row['members_display_name'], 13 );
                $friends[] = $row;
            }
        }
        $member = $this->common->format_member($member_id);
        if(is_array($member) && intval($member['id']) > 0){
            $friend_profile_layout = $this->ipsclass->compiled_templates['skin_regportal']->skin_friend_profile($member);
            echo $friend_profile_layout;
            exit();
        }
    }
}

?>
